2009-06-16 69 views
1

我在ASP.NET應用程序中使用日誌記錄應用程序塊(LAB)以及異常處理應用程序塊來記錄任何未處理的異常。我正在使用Global.asax中的Application_Error方法來捕獲這些錯誤。我正在寫入滾動平面文件。這一切都很好。使用日誌記錄應用程序塊編寫調試信息

當我在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> 

回答

2

我發現,去除using (new Tracer("Debugging"))並將log.Categories.Add("Debugging");添加到上述方法產生了期望的結果。