Sponsored Ad

Saturday, April 17, 2010

How to Log Message in Event Log Using C#

 

How to Log Message in Event Log Using C#

This simple method will help to log your own events into service log viewer. Just call the given method with specific service message and it will add message in Event viewer. This is very helpful in complicated and multithreaded applications to know execution sequence and errors etc.

Call of the method:

ServiceLogMessage("abc");

 

public static void ServiceLogMessage(string strMessage)
    {
        EventLog MyLog = new EventLog();
        // create a new event log
        // Check if the the Event Log Exists
        if (!EventLog.SourceExists("MYServiceLog"))
        {
            EventLog.CreateEventSource("MYServiceLog", "MYServiceLog" + " Log"); // Create Log
        }
        MyLog.Source = "MYServiceLog";
        //' Write to the Log
        EventLog.WriteEntry("MYServiceLog" + " Log", DateTime.Now.ToString() + " " + strMessage, EventLogEntryType.Information);
        MyLog = null;
    }

No comments:

Post a Comment

Sponsored Ad

Development Updates