Enterprise Library: Logging and Instrumentation Application Block概述
Enterprise Library: Logging and Instrumentation Application Block概述<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
?
Written by: Rickie Lee (rickieleemail#yahoo.com)
My blog:http://www.cnblogs.com/rickie
Enterprise Library中的Logging and Instrumentation Application Block讓記錄日志信息到不同的目的位置更加方便,新的Logging Block非常直觀并功能豐富。每個(gè)應(yīng)用程序都會(huì)需要一系列logging功能,并且還需要優(yōu)雅地處理和記錄未捕獲的異常信息。本文簡單介紹了Logging and Instrumentation Application Block的功能特點(diǎn)。
?
Enterprise Library Logging and Instrumentation Application Block讓開發(fā)人員在應(yīng)用程序集成標(biāo)準(zhǔn)的logging and Instrumentation功能,應(yīng)用程序可以使用該Application Block記錄日志事件到不同的位置:
- Event Log (Windows事件日志)
- Email消息
- 數(shù)據(jù)庫
- 消息隊(duì)列(MSMQ)
- 文本文件
- WMI
?
Client-Distributor架構(gòu)
Application Block主要由2部分組成:Client和Distributor。Client負(fù)責(zé)創(chuàng)建消息,這些消息將由Distributor寫入目標(biāo)位置。在多數(shù)情況下,Client和Distributor在同一臺(tái)機(jī)器上,但是你也可以分離這2個(gè)部分到不同的進(jìn)程或不同的機(jī)器上。
Distributor設(shè)置
Client根據(jù)分發(fā)策略(Distribution Strategies)發(fā)送消息到Distributor,在Application Block中提供了2個(gè)分發(fā)策略:In Process和MSMQ,默認(rèn)是In Process策略。你可以刪除In Process strategy,然后添加MSMQ strategy。當(dāng)選擇MSMQ strategy,Client需要?jiǎng)?chuàng)建日志消息并發(fā)送到MSMQ消息隊(duì)列。另外一個(gè)進(jìn)程則等待到達(dá)的消息,并寫入合適的日志接收池(Logging Sink)。
Distributor Service
Application Block中包含一個(gè)Windows Service: MSMQ Distributor Service。當(dāng)安裝該服務(wù)后,該服務(wù)會(huì)在指定的時(shí)間間隔檢測消息隊(duì)列。
?
定制Sink
在某些情況下,也許Logging and Instrumentation Application Block提供的sink(接收池)并不能滿足要求,你也可以創(chuàng)建定制的sink,如下所示:
[C#]
public class DebugSink : LogSink, IConfigurationProvider
?
[Visual Basic]
Public Class DebugSink
? Inherits LogSink
? Implements IConfigurationProvider
定制的sink需要實(shí)現(xiàn)IConfigurationProvider接口,并繼承LogSink抽象類。另外需要增加SendMessageCore方法到定制sink類中,確保實(shí)現(xiàn)必要的動(dòng)作來記錄日志信息到定制的sink。
?
目錄Categories
在Logging and Instrumentation Application Block配置文件中,Distributor Settings下包含有許多目錄Categories,默認(rèn)有Gereral和Trace,如下圖所示。
Logging and Instrumentation Application Block提供靈活的松耦合機(jī)制來記錄日志消息,每一個(gè)日志消息將寫入指定目錄,每一個(gè)目錄可以有0或多個(gè)目標(biāo)存儲(chǔ)位置,目標(biāo)存儲(chǔ)位置指向接收池(sink)和可選的格式化器。
缺省情況下,General目錄設(shè)定為default目錄,并設(shè)計(jì)將消息寫入Application事件日志。你可以通過修改Distributor Settings節(jié)點(diǎn)的DefaultCategory屬性值來改變?nèi)笔〉哪夸浽O(shè)置。你也可以指定缺省的Formatter格式化日志消息。
?
Formatters控制格式化輸出
使用Formatters,Logging Block允許你精確控制消息的輸出格式。Logging Block內(nèi)置了一個(gè)TextFormatter的實(shí)現(xiàn),TextFormatter使用帶Token的模板。你可以通過Configuration Console配置工具創(chuàng)建和管理你的模板。當(dāng)消息準(zhǔn)備寫入接收池(sink)時(shí),Formatter會(huì)使用日志消息屬性值替換每一個(gè)token。如下是默認(rèn)的模板(Property: Token):
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}
)}
Client Settings
Client Settings節(jié)點(diǎn)提供了一系列選項(xiàng),用來控制Logging Block的行為,如LoggingEnabled用來控制Logging行為有效或失效。當(dāng)Logging失效時(shí),可以產(chǎn)生最少的時(shí)間開銷。CategoryFilterSettings屬性允許你配置特定目錄有效或失效(Enable or Disable),你也可以基于數(shù)字優(yōu)先級(numberic priority)來過濾日志消息。在配置設(shè)置中,你可以定義消息分發(fā)的最小優(yōu)先級閥值。具體屬性列表如下所示:
下一篇文章將演示Logging and Instrumentation Application Block的具體應(yīng)用。
***
作者:Rickie Lee (rickieleemail#yahoo.com)
本文參考如下References文檔及其鏈接。
?
References:
1. Microsoft Enterprise Library, Logging and Instrumentation Application Block.
2. Logging with Enterprise Library, http://blog.hishambaz.com/archive/2005/01/30/202.aspx
3. Rickie, Microsoft patterns & practices Enterprise Library January 2005 [中文稿], http://www.cnblogs.com/rickie/archive/2005/01/30/99443.html
4. Rickie, Enterprise Library released! http://www.cnblogs.com/rickie/archive/2005/01/29/99106.html
?
轉(zhuǎn)載于:https://www.cnblogs.com/rickie/archive/2005/02/13/103983.html
總結(jié)
以上是生活随笔為你收集整理的Enterprise Library: Logging and Instrumentation Application Block概述的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 转载:PyBus(排忧巴士)的C#源代码
- 下一篇: 五年后的无线城市