Question

I'm using the Logging Application Block (LAB) in my ASP.NET application along with the Exception Handling Application Block to log any unhandled exceptions. I'm utilizing the Application_Error method in Global.asax to catch these errors. I'm writing to a Rolling Flat File. This all works just fine.

I'd also like to log debug messages using the LAB when I set a switch in the web.config's appSettings section. But I can't figure out how to send these debug messages to a different log file. I'd really appreciate it if someone would look at my code and the section from web.config and see if anything jumps out. Thanks!

Here is an example of how I'm currently trying to write to the debug logger:

private void LogDebugInfo()
{
    using (new Tracer("Debugging"))
    {
        StringBuilder msg = new StringBuilder();
        msg.AppendLine("Querystring: " + Request.QueryString.ToString());

        foreach (string item in Request.Form)
        {
            msg.AppendLine("Form item name: " + item + " value: " + Request.Form[item]);
        }

        HttpFileCollection files = Request.Files;
        foreach (string f in files)
        {
            msg.AppendLine("Posted filename: " + files[f].FileName + " type: " + files[f].ContentType + " length: " + files[f].ContentLength);
        }

        LogEntry log = new LogEntry();
        log.Message = msg.ToString();
        Logger.Write(log);
    }
}

Here is the relevant section from web.config:

<loggingConfiguration name="Logging Application Block" tracingEnabled="true"
  defaultCategory="Data Access" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
        <add fileName="log\Data Access.log" rollSizeKB="0" timeStampPattern="yyyy-MM-dd"
          rollFileExistsBehavior="Increment" rollInterval="Day" formatter="Text Formatter"
          header="----------------------------------------" footer="----------------------------------------"
          listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
          traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
          name="Data Access TraceListener" />
        <add fileName="log\Debugging.log" rollSizeKB="0" timeStampPattern="yyyy-MM-dd"
          rollFileExistsBehavior="Increment" rollInterval="Day" formatter="Text Formatter"
          header="----------------------------------------" footer="----------------------------------------"
          listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
          traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
          name="Debugging TraceListener" />
        <add fileName="log\General Exceptions.log" rollSizeKB="0" timeStampPattern="yyyy-MM-dd"
          rollFileExistsBehavior="Increment" rollInterval="Day" formatter="Text Formatter"
          header="----------------------------------------" footer="----------------------------------------"
          listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
          traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
          name="Log TraceListener" />
    </listeners>
    <formatters>
        <add template="Timestamp: {timestamp}&#xA;Message: {message}&#xA;Category: {category}&#xA;Priority: {priority}&#xA;EventId: {eventid}&#xA;Severity: {severity}&#xA;Title:{title}&#xA;Machine: {machine}&#xA;Application Domain: {appDomain}&#xA;Process Id: {processId}&#xA;Process Name: {processName}&#xA;Win32 Thread Id: {win32ThreadId}&#xA;Thread Name: {threadName}&#xA;Extended Properties: {dictionary({key} - {value}&#xA;)}"
          type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
          name="Text Formatter" />
    </formatters>
    <categorySources>
        <add switchValue="All" name="Data Access">
            <listeners>
                <add name="Log TraceListener" />
            </listeners>
        </add>
        <add switchValue="All" name="Debugging">
            <listeners>
                <add name="Debugging TraceListener" />
            </listeners>
        </add>
        <add switchValue="All" name="General">
            <listeners>
                <add name="Log TraceListener" />
            </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="Log TraceListener" />
            </listeners>
        </errors>
    </specialSources>
</loggingConfiguration>
Was it helpful?

Solution

I have found that removing the using (new Tracer("Debugging")) and adding log.Categories.Add("Debugging"); to the above method produced the desired results.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top