Logging Application Block not logging to a file

Luis Valencia picture Luis Valencia · May 9, 2012 · Viewed 7.7k times · Source

I have the following configuration and code, the writer.logentry is not throwing an exception but the file is not created.

Update 1: I changed the listener, I removed eventlog and added flat file log. I still cant see a created .log file

I suppose I am missing something in the configuration?

<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
    <listeners>
      <add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        fileName="AskAndTrack.log" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, Callstack" />
    </listeners>
    <formatters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
        name="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Flat File Trace Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Flat File Trace Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>

    catch(Exception ex)
    {
        var logEntry = new LogEntry();
        logEntry.Message = ex.Message;
        Logger.Write(logEntry);
    }

Answer

Randy Levy picture Randy Levy · May 10, 2012

The posted configuration looks OK. Enterprise Library will not throw an exception if an error occurs logging -- it will attempt to log the failure to the errors special source (if configured). You haven't added a Trace Listener for errors but I recommend doing so.

Your issue looks like it is probably permissions. Can you verify that the process has permissions to create a file called AskAndTrack.log? Or perhaps create the file ahead of time or write to a folder where the permissions are explicitly granted (e.g. Everyone Full Control (for testing only!)).