tags:

views:

531

answers:

5

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  A: 

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");
Daok
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
Charles Bretana
+1  A: 

You need to create the event source first. Daok's code above should get you going.

Rob Prouse
A: 

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

Miles
A: 

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.

MSDN Link
InstallerClass Link

CheGueVerra
A: 

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?