views:

200

answers:

2

Hi
I'm trying to get my .Net Windows Service to right to a custom event log. I'm using EventLogInstaller to create the event log and source when the application is installed. I read here that it takes a while for Windows to register the source so they reccomend you restart the application before trying to write to the log.

As this is a Windows Service I didn't want to have to force a computer restart or get the user to manually start the service up, so I use this code to wait for the log to exist and then start the service automatically.

while (!(EventLog.Exists("ManageIT") || EventLog.SourceExists("ManageIT Client Service")))
{
    Thread.Sleep(1000);
}

System.ServiceProcess.ServiceController controller = new System.ServiceProcess.ServiceController("ManageIT.Client.Service");
controller.Start();

My problem is that events from the service are still written to the Application Log and although I can see my custom log in the Registry Editor it does not show up in the Windows 7 Event Viewer.

Any help will be much appreciated.

A: 

Try this snippet:

if (!EventLog.SourceExists("MyApplicationEventLog"))
{
    EventSourceCreationData eventSourceData = new EventSourceCreationData("MyApplicationEventLog", "MyApplicationEventLog");
    EventLog.CreateEventSource(eventSourceData);
}

using (EventLog myLogger = new EventLog("MyApplicationEventLog", ".", "MyApplicationEventLog"))
{
    myLogger.WriteEntry("Error message", EventLogEntryType.Error);
    myLogger.WriteEntry("Info message", EventLogEntryType.Information);
}
_simon_
Thats just a standard way of writing to the eventlog. My problem is that even though I have a custom eventlog created. The events I write still go into the application log, not my custom one. Even when using EventLog eventLog = new EventLog("ManageIT"); eventLog.Source = "ManageIT Client Service";
TheDuke
A: 

It sounds like you are writing to the event log like this:

 EventLog.WriteEntry("Source", "Message");

This will write to the application log.

If you use the code in simons post with the creation of myLogger, you can specify the name of the Log.

Shiraz Bhaiji