Recent Posts

Thursday, August 21, 2008

How to handle and log Asp.Net Error in Event Viewer ?

You can just use the following to descently handle you ASP.net error messages and log the error in the event viewer .

Also if you want to add the email function in the function.

the following code is the simplest way to handle error messages in ASP.net:


using System;
using System.Diagnostics;


void Page_Error(Object sender, EventArgs args)
{
Response.Write("Error:\n");
Exception e = Server.GetLastError();

EventLog.WriteEntry("Test Web",
"MESSAGE: " + e.Message +
"\nSOURCE: " + e.Source +
"\nFORM: " + Request.Form.ToString() +
"\nQUERYSTRING: " + Request.QueryString.ToString() +
"\nTARGETSITE: " + e.TargetSite +
"\nSTACKTRACE: " + e.StackTrace,
EventLogEntryType.Error);

Trace.Write("Message", e.Message);
Trace.Write("Source", e.Source);
Trace.Write("Stack Trace", e.StackTrace);
Response.Write("Sorry, an error was encountered.");
Context.ClearError();
}

Related Posts by Categories




No comments:

Post a Comment