Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging Events in a Windows Service Program

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?

like image 650
ArmenB Avatar asked Nov 16 '11 23:11

ArmenB


People also ask

How do I create a custom event log for Windows service?

To set up logging to a custom log You must set AutoLog to false in order to use a custom log. Set up an instance of an EventLog component in your Windows Service application. Create a custom log by calling the CreateEventSource method and specifying the source string and the name of the log file you want to create.

Does Windows have an event logging capability?

Windows Event Viewer displays the Windows event logs. Use this application to view and navigate the logs, search and filter particular types of logs, export logs for analysis, and more.

What is event logging service?

Event logging provides a standard, centralized way for applications (and the operating system) to record important software and hardware events. The event logging service records events from various sources and stores them in a single collection called an event log.

How does Windows event logging work?

At their core, Windows event logs are records of events that have occurred on a computer running the Windows operating system. These records contain information regarding actions that have taken place on the installed applications, the computer, and the system itself.


2 Answers

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.

like image 130
alphadogg Avatar answered Oct 05 '22 14:10

alphadogg


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");     } 
like image 32
James Westgate Avatar answered Oct 05 '22 14:10

James Westgate