Making an entry in Windows XP/2000's event log

  • Thread starter Thread starter Simon Harvey
  • Start date Start date
S

Simon Harvey

Hi,

Does anyone know of the top of their head, how I would add an entry to the
event logs in XP/2000 machines?

Thanks all

Simon
 
System.Diagnostics.EventLog.WriteEntry("MyApp", "Hi there")

The are more examples in the help, search for "EventLog class".
 
You can even register your own event source and create your custom event log.

The eventlogs are differentiated by the first 8 characters of their name, so be careful about that.

The code that creates your custom event log and registers event source must be run with appropriate access rights to EventLog keys in registry. You need to have full access rights to the following key:

HKLM\System\CurrentControlSet\Service\EventLog
(use regedt32.exe to set permissions)

The following code registers your custom event source and creates the log:

System.Diagnostics.EventLog el = new System.Diagnostics.EventLog ();
if (!System.Diagnostics.EventLog.SourceExists (strSource))
System.Diagnostics.EventLog.CreateEventSource (strSource, strLog);
else if (System.Diagnostics.EventLog.LogNameFromSourceName (strSource, ".") != strLog)
{
System.Diagnostics.EventLog.DeleteEventSource (strSource);
System.Diagnostics.EventLog.CreateEventSource (strSource, strLog);
}

Another quite convinient solution is to use the VS.Net designer, create a component class, and use the EventLog component from the component Toolbox. Then you can even easily add automatic intsallers to the project.
 
Back
Top