[zh] http://www.microsoft.com/china/MSDN/library/enterprisedevelopment/softwaredev/dnpag2logging.mspx?mfr=true
[en] http://msdn2.microsoft.com/en-us/library/ms998162.aspx
開發人員經常編寫需要日志和規范功能的應用程序。通常,這些應用程序必須適當地格式化事件和記錄事件,不論是在本地還是通過網絡。在某些情況下,您可能需要對一臺計算機上來自多個源的事件進行整理。
日志應用程序塊通過收集應用程序需要包含的多個最常見的日志和規范任務來簡化應用程序的開發。每個任務都以一致的方式處理,并從特定的日志和規范提供程序中抽象應用程序代碼。體系結構模型可讓您通過更改配置來更改基礎事件接收器和格式化程序,而無需更改應用程序代碼。
1.應用程序可以使用日志塊在多個位置記錄事件:
(1) 事件日志
(2) 電子郵件
(3) 數據庫
(4) 消息隊列
(5) 文件
(6) WMI
2. 使用
(0) EntLib配置工具配置App.config/Web.config,New->Logging Application Block:
a. 創建或定制Formatter(默認只有一個Text Formatter,我們可以定制其Template);
b. 創建或定制Trace Listeners(默認只有一個Fomatted EventLog TraceListener,其可以將日志記錄在系統日志中),并為其指定一個Formatter(上一步a中定義了的Formatter);不同的Trace Listener記錄事件的位置(Email、EventLog、File、DB、MSMQ、WMI)不同。
c. 創建Category Source(默認只有一個General Category,其使用EventLog TraceListener將日志記錄在系統日志中),并為其指定Trace Listener(上一步b中定義了的Trace Listener).
(1) 記錄日志:
LogEntry log = new LogEntry();
log.EventId = 300;
log.Message = "Sample message";
logEntry.Categories.Clear();
log.Categories.Add("CategoryName");//參數為上面步驟c中定義的Category.Name,可以Add多個Category
log.Severity = TraceEventType.Information;
log.Priority = 5;
Logger.Write(log);
(2) 日志里面包含名-值對的字典
Dictionary<string, object> dictionary = new Dictionary<string, object>();
ManagedSecurityContextInformationProvider informationHelper = new ManagedSecurityContextInformationProvider();
informationHelper.PopulateDictionary(dictionary);
dictionary.Add("logInforName", strLogInfor);
Logger.Write("Log entry with extra information", dictionary);//使用默認的Genera Category,記錄事件到系統日志中
(3) 跟蹤活動并記錄上下文信息
LAB支持通過活動ID來跟蹤一項活動,活動ID可以在代碼中指定,也可以通過程序塊來自動生成,LAB自動記錄活動的起始時間和結束時間:
using (new Tracer("Category1"))
{
using (new Tracer("Category2"))
{
LogEntry logEntry = new LogEntry();
//給LogEntry的屬性賦值
Logger.Write(logEntry);//此時,logEntry會寫5條日志:
//1. Start Trace: Activity 'ActivityID'
.. at .. ticks
// Category: Category1
//2. Start Trace: Activity 'ActivityID(同上)'
.. at .. ticks
// Category: Category2, Category1
//3. Simulated General
..Activity='ActivityID(同上)'
// Category: General, Category2, Category1
//4. End Trace: Activity 'ActivityID(同上)'
at
ticks
// Category: Category2, Category1
//5. End Trace: Activity 'ActivityID(同上)'
at
ticks
// Category: Category1
}
}(4) 創建過濾事件
Filters->New->可以創建Category Filter/Custom Filter/LogEnabled Filter/Priority Filter
Category Filter:根據Category的類別進行過濾(Deny all except: someCategory);
Priority Filter:只記錄優先級在MinimumPriority和MaxmumPriority之間的事件;
LogEnabled Filter:記錄(Enabled=true)或不記錄(Enabled=false)所有的LogEntry。
if (Logger.GetFilter<LogEnabledFilter>().Enabled)
{
// Logging is enabled.
}
else
{
// Logging is not enabled.
}
if (Logger.GetFilter<CategoryFilter>().ShouldLog(categories))//ICollection<string> categories;
{
// Event will be logged.
}
else
{
// Event will not be logged.
}
if (Logger.GetFilter<PriorityFilter>().ShouldLog(priority))//int priority;
{
// Event will be logged.
}
else
{
// Event will not be logged.
}
if (Logger.ShouldLog(logEntry))//匯總上面的過濾結果,判斷是否應該過濾掉該事件
{
// Perform possibly expensive operations gather information for the event to be logged.
}
else
{
// Event will not be logged.
}
3. Logging Application Block的設計:

浙公網安備 33010602011771號