title: EntLib V6 Logging within a Windows Service
body: <p>I have a windows service which uses ELAB as logging. When running the service in console mode all logging works fine. But as soon as I start it as a service no logging occurs. </p>
  
  <p>Things I have checked:</p>
  
  <ul>
  <li>checked folder permissions and </li>
  <li>ran the service in console mode from the user which is used by the service</li>
  <li>used PROCMON to check if there was even a try to create a logfile</li>
  <li>used absolute and relative path to specify the logfile</li>
  </ul>
  
  <p>I have no clue why this does not work.</p>
  
  <p>To initialize logging:</p>
  
  <pre><code>try
  {
      var config = new SystemConfigurationSource(true, 30000);
      var logWriterFactory = new LogWriterFactory(config);
      LogWriter = logWriterFactory.Create();
      Logger.SetLogWriter(LogWriter, false);
  }
  catch (Exception ex)
  {
      var msg = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} Error loading configuration. {ex}";
      File.WriteAllText(Environment.ExpandEnvironmentVariables("%TEMP%\\mayflower_logging_error.txt"), msg);
      Debug.WriteLine(msg);
  }
  </code></pre>
  
  <p>And my ELAB configuration:</p>
  
  <pre><code>&lt;enterpriseLibrary.ConfigurationSource selectedSource="Mayflower"&gt;
      &lt;sources&gt;
          &lt;add name="Mayflower"
               type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /&gt;
      &lt;/sources&gt;
  &lt;/enterpriseLibrary.ConfigurationSource&gt;
  &lt;loggingConfiguration name="ServiceLogging" tracingEnabled="true" defaultCategory="General"
                        logWarningsWhenNoCategoriesMatch="false" revertImpersonation="false"&gt;
      &lt;listeners&gt;
          &lt;add name="Logfile"
               type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
               listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
               fileName="Logs\Mayflower-Server.log" footer="::: Message End :::"
               formatter="Detailed" header="" rollFileExistsBehavior="Increment"
               rollInterval="Midnight" rollSizeKB="50000" traceOutputOptions="LogicalOperationStack, DateTime"
               asynchronous="true" /&gt;
          &lt;add
              listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              type="Mayflower.Server.Core.ConsoleTraceListener, Mayflower.Server.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
              filter="Information" name="Console" formatter="Short" /&gt;
      &lt;/listeners&gt;
      &lt;formatters&gt;
          &lt;add
              type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              template="{timestamp} {category} {message}{newline}    Priority: {priority} EventId: {eventid} Severity: {severity} Thread Name: {threadName}{newline}    Extended Properties: {dictionary({key} - {value}{newline})}"
              name="Detailed" /&gt;
          &lt;add
              type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              template="{timestamp(FixedFormatTime)} {category} {message}{newline}"
              name="Short" /&gt;
      &lt;/formatters&gt;
      &lt;categorySources&gt;
          &lt;add switchValue="All" name="General"&gt;
              &lt;listeners&gt;
                  &lt;add name="Console" /&gt;
                  &lt;add name="Logfile" /&gt;
              &lt;/listeners&gt;
          &lt;/add&gt;
      &lt;/categorySources&gt;
      &lt;specialSources&gt;
          &lt;allEvents switchValue="All" name="All Events"&gt;
              &lt;listeners&gt;
                  &lt;add name="Logfile" /&gt;
                  &lt;add name="Console" /&gt;
              &lt;/listeners&gt;
          &lt;/allEvents&gt;
          &lt;notProcessed switchValue="All" name="Unprocessed Category"&gt;
              &lt;listeners&gt;
                  &lt;add name="Console" /&gt;
                  &lt;add name="Logfile" /&gt;
              &lt;/listeners&gt;
          &lt;/notProcessed&gt;
          &lt;errors switchValue="All" name="Logging Errors &amp;amp; Warnings"&gt;
              &lt;listeners&gt;
                  &lt;add name="Logfile" /&gt;
                  &lt;add name="Console" /&gt;
              &lt;/listeners&gt;
          &lt;/errors&gt;
      &lt;/specialSources&gt;
  &lt;/loggingConfiguration&gt;
  </code></pre>