2013-05-29 39 views
6

在我的Web應用程序中,我嘗試創建日誌文件以記錄錯誤和異常,但是當我運行我的應用程序時,日誌文件未在我的解決方案文件夾或bin中創建夾。使用企業庫創建滾動平面日誌文件

我用下面的代碼。請幫我解決這個問題,提前謝謝。

命名空間中使用

using Microsoft.Practices.EnterpriseLibrary.Logging; 

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; 

.cs文件

 public int GetdeskKMasterRecordsCount(string CircleId, string StoreId) 
     { 
      try 
      { 
       throw new Exception("this is normal exception"); 

      } 
      catch (Exception ex) 
      { 
       BindLog(ex); 
       return 0; 
      } 
     } 


     public static void BindLog(Exception ex) 
     { 
      if (ex == null) return; 
      Logger.Write(LogInformation(1, DateTime.Now, ex.Message, " ")); 

     } 

     public static LogEntry LogInformation(int eventId, DateTime timeStamp, string message, string documentName) 
     { 
      LogEntry logEntryObject = new LogEntry(); 
      logEntryObject.EventId = eventId; 
      logEntryObject.Title = documentName; 
      logEntryObject.TimeStamp = timeStamp; 
      logEntryObject.MachineName = System.Environment.MachineName; 
      logEntryObject.Message = message; 
      logEntryObject.Categories.Add("Exception"); 

      return logEntryObject; 
     } 

Web配置文件

<configSections> 
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> 
    <section name="recordingWindowGroup" type="Vodafone.DMS.BAL.WindowConfigurationHandler, Vodafone.DMS.BAL"/> 
    <section name="defaultParamGroup" type="Vodafone.DMS.BAL.DefaultParamConfiguration, Vodafone.DMS.BAL"/> 
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> 
    </configSections> 
    <loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true"> 
    <listeners> 
     <add name="Rolling Flat File Trace Listener" 
      type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
      listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
      fileName="Vodafone.DMS.log.exclude" footer="---------------------------" formatter="Text Formatter" header="---------------------------" rollFileExistsBehavior="Increment" 
      rollSizeKB="10" timeStampPattern="yyyy-MM-dd hh:mm:ss" maxArchivedFiles="7" traceOutputOptions="Timestamp, Callstack" filter="All"/> 
    </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;" 
      type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Text Formatter"/> 
    </formatters> 
    <categorySources> 
     <add switchValue="All" name="General"> 
     <listeners> 
      <add name="Rolling Flat File Trace Listener"/> 
     </listeners> 
     </add> 
    </categorySources> 
    <specialSources> 
     <allEvents switchValue="All" name="All Events"> 
     <listeners> 
      <add name="Rolling Flat File Trace Listener"/> 
     </listeners> 
     </allEvents> 
     <notProcessed switchValue="All" name="Unprocessed Category"> 
     <listeners> 
      <add name="Rolling Flat File Trace Listener"/> 
     </listeners> 
     </notProcessed> 
     <errors switchValue="All" name="Logging Errors &amp; Warnings"> 
     <listeners> 
      <add name="Rolling Flat File Trace Listener"/> 
     </listeners> 
     </errors> 
    </specialSources> 
    </loggingConfiguration> 

回答

0

您需要在您的LogEntry上設置一個類別以匹配配置文件中的類別源(您發佈的配置中的「常規」)。如果您打算記錄異常,而不是編寫處理自己創建LogEntry的代碼,我還會建議查看異常處理應用程序塊。

4

對於企業庫version 5.0 and above,Microsoft已發佈企業庫配置tool,允許您在configuration file中直觀地更改配置。您也可以從NuGet安裝此工具,並且可以從Visual Studio Tools >> Library Package Manager >> Manage NuGet Packages for solution中進行訪問,並添加相應的EntLib Application Block。你可以從here找到指南。

使用我已生成的配置文件,該文件是進行如下的工具:

地點此Web.Config中或app.config文件

<loggingConfiguration name="loggingConfiguration" tracingEnabled="true" 
    defaultCategory="General" logWarningsWhenNoCategoriesMatch="true"> 
    <listeners> 
     <add name="Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
     listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
     fileName="RollingFlatFile.log" 
     footer="---------------------------" formatter="Text Formatter" 
     header="---------------------------" rollFileExistsBehavior="Increment" 
     rollInterval="Week" rollSizeKB="20000" timeStampPattern="yyyy-MM-dd hh:mm:ss" 
     maxArchivedFiles="7" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack" 
     filter="All" /> 
    </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}&#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;" 
     name="Text Formatter" /> 
    </formatters> 
    <categorySources> 
     <add switchValue="All" name="General"> 
     <listeners> 
      <add name="Rolling Flat File Trace Listener" /> 
     </listeners> 
     </add> 
    </categorySources> 
    <specialSources> 
     <allEvents switchValue="All" name="All Events"> 
     <listeners> 
      <add name="Rolling Flat File Trace Listener" /> 
     </listeners> 
     </allEvents> 
     <notProcessed switchValue="All" name="Unprocessed Category"> 
     <listeners> 
      <add name="Rolling Flat File Trace Listener" /> 
     </listeners> 
     </notProcessed> 
     <errors switchValue="All" name="Logging Errors &amp; Warnings"> 
     <listeners> 
      <add name="Rolling Flat File Trace Listener" /> 
     </listeners> 
     </errors> 
    </specialSources> 
    </loggingConfiguration> 

我建議代替產生的日誌文件在應用程序的bin目錄中,您應該指定一個物理位置,方法是將的值更改爲類似fileName="c:\logs\RollingFlatFile.log"

-2
IConfigurationSource configurationSource = ConfigurationSourceFactory.Create(); 
LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource); 
Logger.SetLogWriter(logWriterFactory.Create(), false); 
Logger.Write("btnStartProcess_Click"); 
Logger.Reset(); 
+0

一般而言,您想發佈的不止是代碼轉儲。解釋爲什麼這會工作/等。您將通過這樣做來幫助OP和未來的讀者。 – royhowie

相關問題