46

I have created a Windows service program and I want my error to strictly be written to the Windows eventLog. So I followed these steps from code project article:

http://www.codeproject.com/KB/dotnet/simplewindowsservice.aspx

But I don't see any of the custom log messages I wrote in the event logs created in the event viewer window when I start or stop the service. Also how do I specify whether the message was due to an error or is just info?

unicorn2
  • 844
  • 13
  • 30
ArmenB
  • 2,125
  • 3
  • 23
  • 47

2 Answers2

72

First, MSDN is your friend. Make sure you check out the link, as there are some potential gotchas worth knowing.

Essentially, you create an EventLog object:

this.ServiceName = "MyService";
this.EventLog = new System.Diagnostics.EventLog();
this.EventLog.Source = this.ServiceName;
this.EventLog.Log = "Application";

You also need to create a source, if the above source doesn't exist:

((ISupportInitialize)(this.EventLog)).BeginInit();
if (!EventLog.SourceExists(this.EventLog.Source))
{
    EventLog.CreateEventSource(this.EventLog.Source, this.EventLog.Log);
}
((ISupportInitialize)(this.EventLog)).EndInit();

and then simply use it:

this.EventLog.WriteEntry("My Eventlog message.", EventLogEntryType.Information);

it's actually pretty simple.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
alphadogg
  • 12,762
  • 9
  • 54
  • 88
  • 10
    Note that you will need to have correct permissions to actually create the log. Otherwise you will get an exception (as of at least Windows Server 2003) – Bryan Crosby Nov 16 '11 at 23:47
  • 1
    Definitely, and that is clearly noted in the MSDN documentation. – alphadogg Nov 17 '11 at 01:42
  • 12
    @alphadogg the property of EventLog of a ServiceBase is readonly. The code is wrong. By default, .NET based Windows Service will write the event log as "Application", so you don't need to specifiy it manually. – Eriawan Kusumawardhono Feb 20 '14 at 04:26
  • 5
    Could you please explain why `ISupportInitialize` is used in your code? That is, why do you need to enclose the code within a BeginInit()/EndInit() pair? Thank you. – Sabuncu May 24 '14 at 08:22
  • 1
    Anyone know where we can find/view this log? – James L. Aug 22 '19 at 18:20
  • You can read the logs using the Event Viewer app (it will show under Windows Logs > Application). – victorlin Oct 25 '19 at 20:23
29

I finally got this to work by combining various StackOverflow answers and from MSDN.

First include the following namespaces

using System.ComponentModel;
using System.Diagnostics;

Then setup logging in your constructor

    public UserService1() 
    {
        //Setup Service
        this.ServiceName = "MyService2";
        this.CanStop = true;
        this.CanPauseAndContinue = true;

        //Setup logging
        this.AutoLog = false;

        ((ISupportInitialize) this.EventLog).BeginInit();
        if (!EventLog.SourceExists(this.ServiceName))
        {
            EventLog.CreateEventSource(this.ServiceName, "Application");
        }
        ((ISupportInitialize) this.EventLog).EndInit();

        this.EventLog.Source = this.ServiceName;
        this.EventLog.Log = "Application";
    }

Use as follows:

    protected override void OnStart(string[] args)
    {
        base.OnStart(args);

        this.EventLog.WriteEntry("In OnStart");
    }
James Westgate
  • 11,306
  • 8
  • 61
  • 68