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?
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.
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.
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.
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.
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.
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"); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With