Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most reliable way to create a custom event log and event source during the installation of a .Net Service

Tags:

I am having difficulty reliably creating / removing event sources during the installation of my .Net Windows Service.

Here is the code from my ProjectInstaller class:

// Create Process Installer ServiceProcessInstaller spi = new ServiceProcessInstaller(); spi.Account = ServiceAccount.LocalSystem;  // Create Service ServiceInstaller si = new ServiceInstaller(); si.ServiceName = Facade.GetServiceName(); si.Description = "Processes ..."; si.DisplayName = "Auto Checkout"; si.StartType = ServiceStartMode.Automatic;  // Remove Event Source if already there if (EventLog.SourceExists("AutoCheckout"))     EventLog.DeleteEventSource("AutoCheckout");  // Create Event Source and Event Log      EventLogInstaller log = new EventLogInstaller(); log.Source = "AutoCheckout"; log.Log = "AutoCheckoutLog";  Installers.AddRange(new Installer[] { spi, si, log }); 

The facade methods referenced just return the strings for the name of the log, service, etc.

This code works most of the time, but recently after installing I started getting my log entries showing up in the Application Log instead of the custom log. And the following errors are in the log as well:

The description for Event ID ( 0 ) in Source ( AutoCheckout ) cannot be found. The local computer may not have the necessary registry information or message DLL files to display messages from a remote computer. You may be able to use the /AUXSOURCE= flag to retrieve this description; see Help and Support for details.

For some reason it either isn't properly removing the source during the uninstall or it isn't creating it during the install.

Any help with best practices here is appreciated.

Thanks!

In addition, here is a sample of how I am writing exceptions to the log:

// Write to Log EventLog.WriteEntry(Facade.GetEventLogSource(), errorDetails, EventLogEntryType.Error, 99); 

Regarding stephbu's answer: The recommended path is an installer script and installutil, or a Windows Setup routine.

I am using a Setup Project, which performs the installation of the service and sets up the log. Whether I use the installutil.exe or the windows setup project I believe they both call the same ProjectInstaller class I show above.

I see how the state of my test machine could be causing the error if the log isn't truly removed until rebooting. I will experiment more to see if that solves the issue.

Edit: I'm interested in a sure fire way to register the source and the log name during the installation of the service. So if the service had previously been installed, it would remove the source, or reuse the source during subsequent installations.

I haven't yet had an opportunity to learn WiX to try that route.

like image 898
Jason Stevenson Avatar asked Sep 22 '08 15:09

Jason Stevenson


People also ask

How do I create a custom event log?

To create special log views, Click on the Administrative events. Click on Create Custom View on the right side of the window to open Create Custom View window. Under the Filter, there is Logged drop-down list. You can either choose an appropriate predefined time or use a custom time range for your Custom log views.

How do I create a new event log source?

To create an event source, you need to have a name for your new source (called the Event Source Name) and the name of the log where the event source will be a part. If the event log entries would be written to the standard “Application”, “System” or “Security” logs, then you can use that as the name of the log.

Which of the following methods can be used in C# to create an entry in the system event log?

Use the WriteEvent and WriteEntry methods to write events to an event log.

What are the 3 types of logs available through the Event Viewer?

Types of Event Logs They are Information, Warning, Error, Success Audit (Security Log) and Failure Audit (Security Log).


1 Answers

The ServiceInstaller class automatically creates an EventLogInstaller and puts it inside its own Installers collection.

Try this code:

ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller(); serviceProcessInstaller.Password = null; serviceProcessInstaller.Username = null; serviceProcessInstaller.Account = ServiceAccount.LocalSystem;  // serviceInstaller ServiceInstaller serviceInstaller = new ServiceInstaller(); serviceInstaller.ServiceName = "MyService"; serviceInstaller.DisplayName = "My Service"; serviceInstaller.StartType = ServiceStartMode.Automatic; serviceInstaller.Description = "My Service Description"; // kill the default event log installer serviceInstaller.Installers.Clear();   // Create Event Source and Event Log      EventLogInstaller logInstaller = new EventLogInstaller(); logInstaller.Source = "MyService"; // use same as ServiceName logInstaller.Log = "MyLog";  // Add all installers this.Installers.AddRange(new Installer[] {    serviceProcessInstaller, serviceInstaller, logInstaller }); 
like image 131
helb Avatar answered Sep 18 '22 12:09

helb