Sunday, 24 July 2011

Retrieve error message from event log in ASP.NET2.0(C#)


To retrieve error message or warning message from Windows event log in ASP.NET 2.0 and C# is very simple. This tutorial will show you how to get the error message from event log.

At first, import the namespace of System.Diagnostics

using System.Diagnostics;

Create Eventlog object

EventLog objEventLog = new EventLog("System");

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

Use looping to retrieve all error message from event log

If you want get the information or warning messages from event log, just use EventLogEntryType.Information or EventLogEntryType.Warning to replace EventLogEntryType.Error

foreach (EventLogEntry objEntry in objEventLog.Entries)
{
if(objEntry.EntryType==EventLogEntryType.Error)
{
Response.Write(objEntry.TimeGenerated+"-"+objEntry.Source+"- "+objEntry.Message+"<br>");
}
}

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

The flow for the code behind page is as follows.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Diagnostics;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
EventLog objEventLog = new EventLog("System");

foreach (EventLogEntry objEntry in objEventLog.Entries)
{
if(objEntry.EntryType==EventLogEntryType.Error)
{
Response.Write(objEntry.TimeGenerated+"-"+objEntry.Source+"- "+objEntry.Message+"<br>");
}
}
}
}


Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment