Logging Application Block not logging to a file

7.9k Views Asked by At

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);
    }
2

There are 2 best solutions below

2
On BEST ANSWER

Look to me like you are missing the category and priority, here is my snippet. Ignore the helper methods they just return a category such as your 'General' and a priority such as 'Error'.

                if (!Logger.Writer.IsLoggingEnabled()) return;

            var logEntry = new LogEntry { Severity = GetTraceEventTypeFromPriority(severity) };
            logEntry.Categories.Add(GetLogCategory(app_name, severity)); // CHANGED TO NONE BECAUSE SITECORE SUCKS
            logEntry.Priority = (int)severity;

            if (!Logger.ShouldLog(logEntry)) return;

            logEntry.Message = message;
            logEntry.Title = GetLogTitle(RequestDataManager.RequestData.blah, message, severity);

            lock (_locker)
            {
                Logger.Write(logEntry);
            }
0
On

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!)).