Search code examples
.netloggingenterprise-library

Enterprise Library Logging 4.1 does not write to errors listener when file is inaccessible


We have Enterprise Library 4.1 logging set up to write to the Event Log when regular logging fails. This works fine when logging is configured incorrectly (e.g. an invalid character in the listener fileName attribute), but nothing is written to the event log when the configured file cannot be written to due to invalid permissions.

FWIW here's the config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </configSections>
  <loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add source="Enterprise Library Logging" formatter="Event Log Formatter" machineName="."
           log="Application"
           listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
           traceOutputOptions="None" filter="All"
           type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
           name="Formatted EventLog TraceListener" />
      <add formatter="Text Formatter"
        fileName="d:\trace.log" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Text TraceListener" />
    </listeners>
    <formatters>
      <add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Event Log Formatter" />
      <add template="[{timestamp(yyyy-MM-dd HH:mm:ss,fff)}] {severity} {category}: {message}"
        type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Text Formatter" />
    </formatters>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category">
        <listeners>
          <add name="Text TraceListener" />
        </listeners>
      </notProcessed>
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Formatted EventLog TraceListener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
</configuration>

Anybody solved this one?


Solution

  • You mention that you're seeing this behaviour when there are issues regarding invalid permissions; while there isn't any specific documentation regarding this, there is information around read/write access to a file:

    The issue seems to be that Enterprise Library doesn't consider being unable to write to a log file because it's read-only as an error. This is stated in the following documentation from MSDN:

    Table 4: Flatfile TraceListener Properties

    Note:

    If the file you specify for the FlatfileTraceListener is read-only, the trace listener does not write the data to the file and no exception occurs. Make sure that the file attributes are set to read/write.

    So because it doesn't consider it an error, it won't get picked up by the special category "Logging Errors & Warnings", and therefore won't be written to the Event Log (in your case).

    While the documentation doesn't specifically state the same will occur for permission issues, I've tired both using your configuration file (read-only, and no permission respectively) and the same behaviour is exhibited.

    Unfortunately it seems you have little option but to make sure the file is writeable and that the user executing your application has the correct permissions to write to it.