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

Enterprise Library 實戰系列(八):記錄與檢測應用程式區塊——進階篇

     編輯:關於.NET

前言

專案趕工導致延遲發布,請見諒。本篇涵蓋:字典式日誌、活動追蹤、事件過濾、格式客製化、自訂 Sink 與 Formatter。

一、在日誌中包含名稱-值對字典

LogEntry log = new LogEntry();
Hashtable logtable = new Hashtable();
logtable["key1"] = "value1";
logtable["key2"] = "value2";
logtable["key3"] = "value3";

log.Message = this.txt_LogMessage.Text;
log.Category = Category.General;
log.Priority = Priority.Normal;

Logger.Write(log, logtable);

字典輸出範例

二、追蹤活動並記錄上下文

使用 Tracer 類別自動記錄開始/結束時間:

using (new Tracer("UI Events"))
{
    Logger.Write("Troubleshooting message");
}

活動追蹤輸出

三、過濾事件

兩種過濾方式:

  • 優先級過濾:設定最低優先級,低於者不記錄
    優先級過濾
  • 類別過濾:指定記錄或排除特定類別
    類別過濾設定 排除 UI Events

四、客製化日誌訊息格式

Formatter 接收 LogEntry 輸出格式化字串。Text Formatter 使用範本,{參數} 由 LogEntry 屬性填充。
Category 對應 Formatter/Sink Text Formatter 範本 範本編輯器

五、建立與使用自訂 Sink

  1. 參考組件:Microsoft.Practices.EnterpriseLibrary.Configuration.dll、Logging.dll、Logging.Sinks.dll、Logging.Distributor.Configuration.dll
  2. 命名空間:Microsoft.Practices.EnterpriseLibrary.Configuration、Logging、Logging.Sinks、Logging.Distributor.Configuration
  3. 繼承 LogSink,實作 Initialize、SendMessageCore:
public class ConsoleSink : LogSink
{
    private LoggingConfigurationView configView;

    public override void Initialize(ConfigurationView configurationView)
    {
        this.configView = configurationView as LoggingConfigurationView;
    }

    protected override void SendMessageCore(LogEntry logEntry)
    {
        CustomSinkData sinkData = (CustomSinkData)this.configView.GetSinkData(this.ConfigurationName);
        Console.WriteLine((string)sinkData.Attributes["delimiter"]);
        Console.WriteLine(FormatEntry(logEntry));
    }
}
  1. 組態工具新增 Custom Sink,指定 TypeName 為 ConsoleSink
  2. 加入 Attribute:delimiter = ************************
  3. 建立 Destination 關聯 Formatter 與 Sink
  4. 專案輸出類型設為 Console Application 執行

Custom Sink Type Selector Attributes 設定

六、建立與使用自訂 Formatter

  1. 命名空間:System.Globalization、System.IO、System.Xml、Configuration、Logging、Logging.Distributor.Configuration、Logging.Formatters
  2. 繼承 ConfigurationProvider 實作 ILogFormatter,實作 Initialize、Format:
public class XmlFormatter : ConfigurationProvider, ILogFormatter
{
    private LoggingConfigurationView configView = null;

    public override void Initialize(ConfigurationView configurationView)
    {
        this.configView = (LoggingConfigurationView)configurationView;
    }

    public string Format(LogEntry log)
    {
        using (StringWriter sw = new StringWriter(CultureInfo.InvariantCulture))
        {
            XmlTextWriter w = new XmlTextWriter(sw);
            w.Formatting = Formatting.Indented;
            w.Indentation = 2;
            w.WriteStartDocument(true);
            w.WriteStartElement("logEntry");
            w.WriteAttributeString("Category", log.Category);
            w.WriteAttributeString("Priority", log.Priority.ToString(CultureInfo.InvariantCulture));
            w.WriteElementString("Timestamp", log.TimeStampString);
            w.WriteElementString("Message", log.Message);
            w.WriteElementString("EventId", log.EventId.ToString(CultureInfo.InvariantCulture));
            w.WriteElementString("Severity", log.Severity.ToString(CultureInfo.InvariantCulture));
            w.WriteElementString("Title", log.Title);
            w.WriteElementString("Machine", log.MachineName);
            w.WriteElementString("AppDomain", log.AppDomainName);
            w.WriteElementString("ProcessId", log.ProcessId);
            w.WriteElementString("ProcessName", log.ProcessName);
            w.WriteElementString("Win32ThreadId", log.Win32ThreadId);
            w.WriteElementString("ThreadName", log.ManagedThreadName);
            w.WriteEndElement();
            w.WriteEndDocument();
            return sw.ToString();
        }
    }
}

使用方式:組態工具新增 Custom Formatter,指定類型為 XmlFormatter。

結語

進階篇介紹字典日誌、活動追蹤、過濾、格式客製化、自訂 Sink/Formatter。下一篇將介紹快取區塊。

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