tags:

views:

693

answers:

3

I have some code like this:

EventLog.CreateEventSource("myApp", "myAppLog");
EventLog.WriteEntry("myApp", "Test log message", EventLogEntryType.Error);

Now, unless I'm missing something having read MSDN, this should cause a new log 'myAppLog' to be created in the event viewer, and an entry should be added to that new log with the source name 'myApp'. But, I can't get the new log to be created. This always just writes an error log message to the Application log, with the source 'myApp' - 'myAppLog' is nowhere to be seen. What am I doing wrong? I am logged in as an Administrator.

A: 

Did you set the source on your EventLog?

From MSDN Article.

You must set the Source property on your EventLog component instance before you can write entries to a log. When your component writes an entry, the system automatically checks to see if the source you specified is registered with the event log to which the component is writing, and calls CreateEventSource if needed. In general, create the new event source during the installation of your application. This allows time for the operating system to refresh its list of registered event sources and their configuration. If the operating system has not refreshed its list of event sources and you attempt to write an event with the new source, the write operation will fail. If creating the source during installation is not an option, then try to create the source well ahead of the first write operation, perhaps during your application initialization. If you choose this approach, be sure your initialization code is running with administrator rights on the computer. These rights are required for creating new event sources.

David Basarab
That only applies if you're creating an instance of an EventLog. As I'm using the static WriteEntry method, the source is passed as the first string argument.
Jez
A: 

You might be forgetting to set the Source property on your EventLog.

It should look something like this:

        if(!EventLog.SourceExists("MySource"))
        {
            EventLog.CreateEventSource("MySource", "MyNewLog");
        }

        EventLog myLog = new EventLog();
        myLog.Source = "MySource";

        myLog.WriteEntry("Writing to event log.");

Here's the MSDN article for reference.

Joseph
Same mistake as another answer. :-) I'm not using an instance of EventLog; I'm using the static WriteEntry method, so this doesn't apply.
Jez
@Jez if you're creating the EventSource on the fly you have to keep in mind that there is a latency in building the EventSource, so it won't become immediately available. If you execute you're program twice, does it start writing to the correct source?
Joseph
+5  A: 

Is it possible that you already used the source "myApp" when writing to the standard Application log? If so according to MSDN:

If a source has already been mapped to a log and you remap it to a new log, you must restart the computer for the changes to take effect.

http://msdn.microsoft.com/en-us/library/2awhba7a.aspx (about half way down the page)

Chris Haas
This was basically my problem. Windows event log is a bit screwy in that it may not work properly if it's the first time you're writing to a new custom log until you restart the machine. I got it creating the log, but it was still writing messages both to that new custom log and the Application log in the Event Viewer, until I restarted the machine. After that, new messages just got logged to my new custom log. Thanks!
Jez