Create Event Logs using c#
WSEventLog.cs
using System;
using System.Diagnostics;
using System.Security;
namespace CreateDocuments
{
public class WSEventLog
{
string DriveName
= "CreateDocuments";
string LogName =
"Create Documents";
private EventLog
eventLog;
private bool
initialized;
public
WSEventLog(EventLog eventLog)
{
this.eventLog = eventLog;
}
//Check and
create source
public void
Initialize()
{
try
{
try
{
if
(!System.Diagnostics.EventLog.SourceExists(DriveName))
{
System.Diagnostics.EventLog.CreateEventSource(
DriveName, LogName);
}
}
catch { }
eventLog.Source = DriveName;
eventLog.Log = LogName;
initialized = true;
}
catch (SecurityException ex)
{
initialized = false;
eventLog.WriteEntry(ex.Message,
System.Diagnostics.EventLogEntryType.Error);
}
}
//Write error
public void
WriteEntry(string message, System.Diagnostics.EventLogEntryType type =
EventLogEntryType.Information)
{
if
(!initialized)
{
return;
}
try
{
eventLog.WriteEntry(message, type);
}
catch(Exception ex)
{
eventLog.WriteEntry(ex.Message,
System.Diagnostics.EventLogEntryType.Error);
}
}
}
}
Consuming Event Logs
private WSEventLog eventLog;
eventLog = new
WSEventLog(new EventLog());
try
{
}
catch(Exception ex)
{
eventLog.WriteEntry("This
is my first Event log: " + ex.Message,
System.Diagnostics.EventLogEntryType.Error);
}
Comments
Post a Comment