.NET     Console.WriteLine( "All Things .NET" );
.NET Nerd Blog Home
3.14.2002

 

Simple NT Event Logging


global.asax

Application_Start {
string LogName = "DanSource";
if ( EventLog.SourceExists( LogName ) == false )
EventLog.CreateEventSource( LogName, LogName );
}

Application_Error {
string msg = String.Format( "error in {0}, text = {1}",
Request.Path, Server.GetLastError.ToString() );
EventLog evt = new EventLog();
evt.Source = LogName;
evt.WriteEntry( msg, EventLogEntryType.Error );
}


Use this in conjunction with web.config setting of customErrors mode="On".
Users will be redirected to the error page, but you will still log events or send email or whatever with the details of the error.

Deja dudes talked about security issues when trying to write to event log from within asp.net application.
More info here.

Note: Couldn't get this to work from WinForms app. The 2nd param to CreateEventSource is the log where to create the source. If left empty string, this defaults to "Application" log. I couldn't get events to log to the application log. If you provide a log name there, then you get a completely new log, different from application, where the events will indeed show up.



Comments: Post a Comment

Powered by Blogger