Writing to Windows Event Log (C#)

You have written a server-based application. But as is the case with most servers, it runs in background, either as a Windows Service or may be with a hidden window. Now, how are you going to notify the System Administrator, in case some failures or important events occur? I would recommend that you use the Windows Event log for this purpose.
 
using System.Diagnostics;
 
public void WriteEventLog(string sCallerName, string sLogLine)
{
  if (!System.Diagnostics.EventLog.SourceExists(sCallerName))
    System.Diagnostics.EventLog.CreateEventSource(sCallerName, "Application");
 
  EventLog EventLog1 = new EventLog();
  EventLog1.Source = sCallerName; 
  EventLog1.WriteEntry (sLogLine, EventLogEntryType.Warning);
}
 
Note: Use the relevant EventLogEntryType for your application.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: