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?
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");
You need to create the event source first. Daok's code above should get you going.
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
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.
if (!System.Diagnostics.EventLog.SourceExists(eventLogSource)){ System.Diagnostics.EventLog.CreateEventSource(eventLogSource, eventLogName);}eventLog1.Source = eventLogSource;eventLog1.Log = eventLogName;eventLog1.WriteEntry("This is a test");
is there any way to put that code in some error handling like a try-catch?