首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

Enterprise Library 2.0 - Logging Application Block (下):基本操作與自訂 Formatter、TraceListener

     編輯:關於ASP.NET

承接上篇配置說明,本文介紹 Logging Application Block 的基本操作,以及自訂 Formatter 與 TraceListener 的方法。

將事件日誌寫入文字檔

假設已依上篇步驟完成配置,組態檔內容如下:

<loggingConfiguration name="Logging Application Block" tracingEnabled="true"
    defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
  <listeners>
    <add fileName="trace.log" header="----------------------------------------"
         footer="----------------------------------------" formatter="SHY520 Formatter"
         listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData,
         Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
         traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener,
         Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
         name="SHY520 Listeners" />
    <add source="Enterprise Library Logging" formatter="Text Formatter"
         log="Application" machineName="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData,
         Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
         traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener,
         Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
         name="Formatted EventLog TraceListener" />
  </listeners>
  <formatters>
    <add template="Timestamp: {timestamp} Message: {message} Category: {category} Priority: {priority} EventId:
         {eventid} Severity: {severity} Title:{title} Machine: {machine} Application Domain: {appDomain} Process Id:
         {processId} Process Name: {processName} Win32 Thread Id: {win32ThreadId} Thread Name: {threadName} Extended Properties:
         {dictionary({key} - {value} )}"
         type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0,
         Culture=neutral, PublicKeyToken=null"
         name="Text Formatter" />
    <add template="Timestamp: {timestamp} Message: {message} Category: {category} Priority: {priority} EventId:
         {eventid} Severity: {severity} Title:{title} Machine: {machine} Application Domain: {appDomain} Process Id:
         {processId} Process Name: {processName} Win32 Thread Id: {win32ThreadId} Thread Name: {threadName} Extended Properties:
         {dictionary({key} - {value} )}"
         type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0,
         Culture=neutral, PublicKeyToken=null"
         name="SHY520 Formatter" />
  </formatters>
  <logFilters>
    <add categoryFilterMode="AllowAllExceptDenied" type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter,
         Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
         name="Category Filter" />
  </logFilters>
  <categorySources>
    <add switchValue="All" name="General">
      <listeners>
        <add name="SHY520 Listeners" />
      </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="Formatted EventLog TraceListener" />
      </listeners>
    </errors>
  </specialSources>
</loggingConfiguration>

程式碼撰寫日誌

using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.ExtraInformation;
using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;

// 簡單寫入
Logger.Write("這是一條測試日誌");

// 進階用法:指定類別、優先級、事件 ID 等
LogEntry entry = new LogEntry();
entry.Message = "詳細的日誌訊息";
entry.Categories.Add("General");
entry.Priority = 2;
entry.EventId = 1001;
entry.Severity = TraceEventType.Information;
entry.Title = "測試標題";
Logger.Write(entry);

自訂 Formatter

繼承 LogFormatter 並覆寫 Format 方法,即可實現自訂輸出格式(如 XML、JSON 等)。

自訂 TraceListener

繼承 CustomTraceListener 並覆寫 Write、WriteLine 方法,可將日誌導向任何自訂目的地(如 Web Service、雲端儲存等)。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved