I am trying to use the Event Log to write some debug information and I can't make it works. It complains about not being able to find the Event Source. Do I have to install something on the OS?
3 Answers
Here a code that I modified from one of our application. This might help you to start with the creation.
System.Diagnostics.EventLog eventLog1 = new System.Diagnostics.EventLog();
string eventLogName = "StackOverFlowEventName";
string eventLogSource = "StackOverFlowWebsite";
//This code HERE will create the Event for you
if (!System.Diagnostics.EventLog.SourceExists(eventLogSource))
{
System.Diagnostics.EventLog.CreateEventSource(eventLogSource, eventLogName);
}
eventLog1.Source = eventLogSource;
eventLog1.Log = eventLogName;
eventLog1.WriteEntry("This is a test");
1 Comment
Charles Bretana
Just remember that to run the line of code that creates the Event source requires slightly higher permissions than is required simply to write to the event source pnce it has been created. So if this is on a web site, for e.g., you might want to have deployment package create the event source 4 u
What code are you using currently in your project? I'm currently using this line to write to the event log.
System.Diagnostics.EventLog.WriteEntry(assemblyName, "Error stuff", System.Diagnostics.EventLogEntryType.Error);
Watch out though, this will throw an exception if the log file is full
Comments
You can also go directly in the registry and add the values required to make it work or you could add a project in the solution that will contain only a InstallerClass that will create the EventLog entry for you, when you run the exe it creates.