Saturday, January 10, 2009

How to write Event viewer log using C#

Call the below function, this will create and write into the event viewer.
   1:  private void LogErrorToEventLog() 
   2:  { 
   3:  string sSource; 
   4:  string sLog; 
   5:  string strEvent="Error Occured";
   6:  sSource = "test"; 
   7:  sLog = "Application"; 
   8:  if (!EventLog.SourceExists(sSource)) 
            EventLog.CreateEventSource(sSource,sLog); 
   9:  EventLog.WriteEntry(sSource, 
       strEvent,EventLogEntryType.Error, 234); 
  10:  } 

No comments: