Automatic Logging of ASP.NET App shutdown events

By Peter Bromberg
ODBC Drivers for QuickBooks, Salesforce, SAP, MSCRM, SharePoint … Free Trial!

Scott Guthrie ("Mr. ASP.NET") had this on his blog, written by one of his team. Just add the code to your global.asax.cs file along with the required namespace "using" declarations :

public void Application_End()
{
HttpRuntime runtime =
(HttpRuntime) typeof(System.Web.HttpRuntime).InvokeMember("_theRuntime",
BindingFlags.NonPublic
| BindingFlags.Static
| BindingFlags.GetField,
null,
null,
null);
if (runtime == null)
return;
string shutDownMessage =
(string) runtime.GetType().InvokeMember("_shutDownMessage",
BindingFlags.NonPublic | BindingFlags.Instance| BindingFlags.GetField,
null, runtime, null);
string shutDownStack = (string) runtime.GetType().InvokeMember("_shutDownStack",
BindingFlags.NonPublic | BindingFlags.Instance| BindingFlags.GetField,
null, runtime, null);
if (!EventLog.SourceExists(".NET Runtime"))
{
EventLog.CreateEventSource(".NET Runtime", "Application");
}
EventLog log = new EventLog();
log.Source = ".NET Runtime";
log.WriteEntry(
String.Format("\r\n\r\n_shutDownMessage={0}\r\n\r\n_shutDownStack={1}", shutDownMessage, shutDownStack), EventLogEntryType.Error);
}


Submission Date:  12/16/2005 12:30:24 PM
Submitted By:  Peter Bromberg
My Home Page:  http://www.eggheadcafe.com

Popularity  (207 Views)