views:

431

answers:

2

I've created a run of the mill C# Windows Service using the windows service project template. I've been able to install it correctly, and start and stop it without any issues.

However, when I go to my event viewer to see the log of it starting and stopping, I get nothing.

Here's the sample code I'm testing:

public MyService()
    {
        InitializeComponent();

        ServiceName = "My Data Service";
        EventLog.Log = "Application";
    }

    protected override void OnStart(string[] args)
    {
        EventLog.WriteEntry("Starting My Data Service");
    }

    protected override void OnStop()
    {
        EventLog.WriteEntry("Ending MyData Service");
    }

Also, my OS is Windows Vista.

+1  A: 

EventLog requires an EventSource created to be able to write.

DreamSonic
+2  A: 

If you want it to log to the default log use

EventLog.WriteEntry("Starting My Data Service", EventLogEntryType.Information);

Of course, you have to ensure that the service is running under an account with sufficient privileges to write to the log and to "run as a service".

Found this example on SO, Best Way to write to the event log

Here's an example where you specify the source too, rather than displaying as .NET Runtime... MSDN Example

Simon Wilson