title: What is the most reliable way to create a custom event log and event source during the installation of a .Net Service
body: <p>I am having difficulty reliably creating / removing event sources during the installation of my .Net Windows Service.</p>
  
  <p>Here is the code from my ProjectInstaller class:</p>
  
  <pre><code>// 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 });
  </code></pre>
  
  <p>The facade methods referenced just return the strings for the name of the log, service, etc.</p>
  
  <p>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:</p>
  
  <blockquote>
    <p>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. </p>
  </blockquote>
  
  <p>For some reason it either isn't properly removing the source during the uninstall or it isn't creating it during the install.</p>
  
  <p>Any help with best practices here is appreciated.</p>
  
  <p>Thanks!</p>
  
  <p>In addition, here is a sample of how I am writing exceptions to the log:</p>
  
  <pre><code>// Write to Log
  EventLog.WriteEntry(Facade.GetEventLogSource(), errorDetails, EventLogEntryType.Error, 99);
  </code></pre>
  
  <p><strong>Regarding stephbu's answer:</strong> The recommended path is an installer script and installutil, or a Windows Setup routine.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p><strong>Edit:</strong>
  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.</p>
  
  <p>I haven't yet had an opportunity to learn WiX to try that route.</p>