使用Logging Application Block写入调试信息

时间:2009-06-16 16:55:34

标签: asp.net logging enterprise-library

我在ASP.NET应用程序中使用日志记录应用程序块(LAB)以及异常处理应用程序块来记录任何未处理的异常。我正在利用Global.asax中的Application_Error方法来捕获这些错误。我正在写一篇Rolling Flat File。这一切都很好。

当我在web.config的appSettings部分设置一个开关时,我还想使用LAB记录调试消息。但我无法弄清楚如何将这些调试消息发送到不同的日志文件。如果有人会查看我的代码和web.config中的部分并查看是否有任何内容跳出来,我真的很感激。谢谢!

以下是我目前正在尝试写入调试记录器的示例:

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

以下是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>

1 个答案:

答案 0 :(得分:2)

我发现删除using (new Tracer("Debugging"))并在上述方法中添加log.Categories.Add("Debugging");会产生预期效果。