Wednesday 20 February 2013

How to write error or exception Messages into Event log in C#?

How to write error or exception Messages into Event log in C#?

There are situations where we don't want to show the errors or exceptions to users, in that scenario,  we can write messages into Server's Event Log by using the below function for our debugging purpose

Please replace "YourProjectName" with your's Project Name


public void WriteErrorToEventLog(string strErrMsg)
{
 System.Diagnostics.EventLog objLog= new System.Diagnostics.EventLog();

 if (!EventLog.SourceExists("YourProjectName"))
  
        { EventLog.CreateEventSource("YourProjectName", "Application");}
objLog.WriteEntry("YourProjectName", strErrMsg, System.Diagnostics.EventLogEntryType.Error);
}


Enjoy!!

No comments:

Post a Comment

You might also like:

Related Posts Plugin for WordPress, Blogger...