Writing to Event Logs | | One thing that many programmers need to do is find out information about application problems after their application has been deployed. Thanks to the System.Diagnostics.EventLog class, it's a piece of cake to serialize your exceptions and write them to an event log using the EventLog class' WriteEntry method. You can even write to a log that you create especially for your app.
VB.NET
Imports System.Diagnostics
Try
File.Open("SomeFileThatsOpenAlready")
Catch ex as System.IO.IOException
Dim myLog as EventLog = New EventLog()
myLog.Source = "NameOfMyApplication"
myLog.WriteEntry(ex.ToString)
Debug.Assert(False, ex.Message)
End Try |
C#
using System.Diagnostics
try{
File.Open("SomeOpenFile");
}
catch(System.IO.IOException ex){
EventLog myLog = new EventLog();
myLog.Source = "NameOfMyApplication";
myLog.WriteEntry(ex.ToString);
Debug.Assert(false, ex.Message);
} |
You don't have to use your own event log, but it certainly will make it easier to track down. To use one of the built in ones, System Log|Application Log| Security Log| Directory Log etc, simply use their name for the source. Then, next time you want to see what just happened, it'll be waiting for you in the computer's Event Viewer
|