Change event log in Windows service

I am writing my first Windows service today and after a very helpful pass here: http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx

Everything works fine.

When I was working with the service, I started editing it to get the real code there, which went fine too.

However, I found that if I change the event log name or source in the following code, the service will not start. Returning to the names used in the walkthrough, 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 there is some kind of reference to the service name and log file in the registry, and if they don't match, this will prevent it from starting, so I deleted all registry keys that reference the log.

But it still fails, after deleting the registry keys and restarting the service with the original names, the keys will be recreated. But not created with any other name.

How do I change the name of the LogFile? not a show stopper, but wish it had some link to the Service.

Greetings

EDIT:

Thanks to CodeCaster, I found a related error message.

Source "TwitterService" is not logged in "TwitterLog". (This is registered in the Application log.)

And following this message: Error writing event log I now have a service to start and log correctly.

However, it now logs itself to the Application log and requires the line

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

      

Before checking if it exists to begin with.

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

I've never logged the original (MySource / MyNewLog) anywhere.

EDIT2:

This post explains that a reboot is required to force it to enter the correct location. Windows event log - how to register an event source? - and now it works.

thanks for the help

+3


source to share


2 answers


I can only guess that there is some kind of reference to the service name and log file in the registry, and if they don't match, this will prevent it from starting, so I deleted all registry keys that reference the log.

No, event logs must exist for your application and your process must be run as administrator to create an event log.



If you enter the actual exception text (which is helpful for your question) into the site's search engine, you will find very helpful answers.

+1


source


If you started the service once using the approximate name of the event source, remove the old service first. Then change the source name and reinstall the service. This should do the trick.



Also make sure you change them in the component EventLogInstaller

(if you are using it).

0


source







All Articles