
Navigator : Home > Tutorials > Error Handling Tutorials > ...
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>"); } } |
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>"); } } } } |
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
Looking for the VB.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!