Wrtite Mesasges to Windows Event Viwer

  • Thread starter Thread starter Abra
  • Start date Start date
A

Abra

Hi,
I would like to be able from my C# .NET application to send
(programatically) different debug messages to an own directory in the
standard Windows Event Viewer. Which .NET classes provide access to the
Windows Event Viewer ? Is it possible to configure it to automatically
log the messages into files after a given amount of records ? Is it
possible to have the message time-stamps in milliseconds ?
Some code sample of using such .NET classes would be appreciated.
Thanks in advance for your help.
Regards,
Abra
 
private System.Diagnostics.EventLog Log;

private bool EventLoggerIsOk;

public void Initialize()

{

try

{

Log = new EventLog();

if (!EventLog.SourceExists("Source"))

{

EventLog.CreateEventSource("Source", "LogName");

}

Log.Source = "Source";

Log.Log = "LogName";

Log.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 5);

EventLoggerIsOk = true;

}

catch

{

EventLoggerIsOk = false;

}

}

public System.Diagnostics.EventLogEntryCollection Events

{

get

{

return (Log.Entries);

}

}

private string PrepareMessage(string Message)

{

// Do what ever string function

return (Message);

}

public void LogMessage(string Message)

{

if (EventLoggerIsOk)

{

this.Log.WriteEntry(PrepareMessage(Message),
System.Diagnostics.EventLogEntryType.Information);

}

}

public void LogError(string Message)

{

if (EventLoggerIsOk)

{

this.Log.WriteEntry(PrepareMessage(Message),
System.Diagnostics.EventLogEntryType.Error);

}

}

public void LogWarning(string Message)

{

if (EventLoggerIsOk)

{

this.Log.WriteEntry(PrepareMessage(Message),
System.Diagnostics.EventLogEntryType.Warning);

}

}
 
Thank you for the code sample, it works fine.
Is it is possible to set manually the time displayed for a message in
EventViewer, by setting it when adding a new entry ? Is it possible to
get the message time-stamp in milliseconds ?
Is there any workaround for the "ModifyOverflowPolicy" method in .NET
1.1, as it is implemented only in .NET 2.0 ?
Thanks in advance for your help.
 
Back
Top