Morvael
Morvael

Reputation: 3567

Change the Event Log in a windows service

I'm writing my first windows service today and following the very helpful walk-through here: http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx

Which all works fine.

When I had the service working I started editing it to get my real code in there, which also went fine.

However I have discovered that if I change the name of the event log, or source in the following code, then the service won't start. Changing it back to the names used in the walk-through allow the service to start again.

eventLog1 = new System.Diagnostics.EventLog();
if (!System.Diagnostics.EventLog.SourceExists("TwitterService"))
{
    System.Diagnostics.EventLog.CreateEventSource(
    "TwitterService", "TwitterLog");
}
eventLog1.Source = "TwitterService";
eventLog1.Log = "TwitterLog";

I can only guess that the registry holds some sort of reference to the service name and log file, and unless they match it wont allow it to run, so I have removed all the registry keys referencing the log.

But still no luck, after deleting the registry keys and re-starting the service with the original names the keys are recreated. But not created with any other name.

How do I change the LogFile name? not a show stopper but would like it to have some reference to the Service.

Cheers

EDIT:

Thanks to CodeCaster I have found the error message in question.

The source 'TwitterService' is not registered in log 'TwitterLog'. (It is registered in log 'Application'.)

And following this post: Event log write error I have now got the service to start and log correctly.

However it is now logging to the 'Application' Log, and requires the line

System.Diagnostics.EventLog.DeleteEventSource("TwitterService");

Before checking it exists in order for it to start.

How do I change / set where the TwitterService Source is registered?

I never registered the Original (MySource / MyNewLog) anywhere.

EDIT2:

This post explains that a reboot is required to get it to log in the right place. Windows Event Log - how to register an event source? - and now its working.

Thanks for the help

Upvotes: 3

Views: 3138

Answers (2)

CodeCaster
CodeCaster

Reputation: 151588

I can only guess that the registry holds some sort of reference to the service name and log file, and unless they match it wont allow it to run, so I have removed all the registry keys referencing the log.

No, an event log must exist for your application to be able to write to it, and your process must run with administrative permissions to create an event log.

If you input the actual exception text (which would be useful to be in your question) in the site's search engine, you'll find very useful answers.

Upvotes: 1

Mrchief
Mrchief

Reputation: 76208

If you ran the service once using the sample's event source name, then uninstall the old service first. After that, change the source name and re-install your service. That should do the trick.

Also make sure you change them in the EventLogInstaller component (if you're using one) as well.

Upvotes: 0

Related Questions