Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
在本系列的技巧(1)和技巧(2)中分別介紹了使用外部配置文件,使用數據庫記錄配置信息兩種方法,不知道大家有沒有想過不使用任何配置文件,也不使用數據庫而直接用編程的方法來實現呢?本文將會展示如何使用編程的方法來配置Logging Application Block。首先我們需要了解一下Logging Application Block中比較重要的幾個對象:
1.LogFormatter
格式化對象,LogFormatter有TextFormatter和BinaryFormatter兩種,多數情況下我們會使用TextFormatter,它通過一個Template來創建,一個完整的Template格式如下:
Timestamp:?{timestamp}{newline}
Message:?{message}{newline}
Category:?{category}{newline}
Priority:?{priority}{newline}
EventId:?{eventid}{newline}
Severity:?{severity}{newline}
Title:{title}{newline}
Machine:?{machine}{newline}
Application?Domain:?{appDomain}{newline}
Process?Id:?{processId}{newline}
Process?Name:?{processName}{newline}
Win32?Thread?Id:?{win32ThreadId}{newline}
Thread?Name:?{threadName}{newline}
Extended?Properties:?{dictionary({key}?-?{value})}{newline}
這里就不具體解釋每一項的含義,大家可以參考有關文檔,示例代碼:
const?string?Template?=?"Timestamp:?{timestamp}{newline}"?+
????????????????????????????"Message:?{message}{newline}"?+
????????????????????????????"Category:?{category}{newline}"?+
????????????????????????????"Machine:?{machine}{newline}";
TextFormatter?formatter?=?new?TextFormatter(Template);
2.TraceListener
TraceListener提供了日志記錄服務,它指定的是日志將被記錄到何處,數據庫中或者是文本文件,Enterprise Library提供了7種
TraceListener:Database TraceListener、Email TraceListener、Flat File TraceListener、Formatter Event Log TraceListener、Msmq TraceListener、System Diagnostics TraceListener、WMI Trace Listener。每一種TraceListener都需要一個LogFormatter來對記錄的信息進行格式化,例如創建一個FlatFileTraceListener實例:
const?string?LogFilePath?=?@"d:\\share\\messages.log";FlatFileTraceListener?logFileListener?=
????????????new?FlatFileTraceListener(LogFilePath,
???????????????????????????????????????"----------",
???????????????????????????????????????"----------",
???????????????????????????????????????formatter);
這里的formatter就是在上面創建的TextFormatter對象。
3.LogSource
LogSource其實就是TraceListener的集合,Enterprise Library允許針對不同的日志信息記錄到不同地方,因此可以在LogSource中加入多個TraceListener:
LogSource?mainLogSource?=????????????new?LogSource("MainLogSource",?SourceLevels.All);
????????mainLogSource.Listeners.Add(logFileListener);
4.LogFilter
過濾器,對日志信息進行過濾,Enterprise Library默認提供了三種過濾器,用戶也可以定義自己的過濾器,示例代碼:
//?創建一個類別過濾器ICollection<string>?categoryfilters?=?new?List<string>();
categoryfilters.Add(DebugCategory);
CategoryFilter?categoryFilter?=?new?CategoryFilter("CategoryFilter",?categoryfilters,?CategoryFilterMode.AllowAllExceptDenied);
?
//?加入類別過濾器到集合中
ICollection<ILogFilter>?filters?=?new?List<ILogFilter>();
了解了這四個對象,其實我們就已經知道了該如何去用編程的方法配置Logging Application Block,下面給出一個簡單的例子,先寫一個MyLogger靜態類:
using?System;using?System.Collections.Generic;
using?System.Diagnostics;
using?Microsoft.Practices.EnterpriseLibrary.Logging;
using?Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using?Microsoft.Practices.EnterpriseLibrary.Logging.Filters;
using?Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;
using?Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
public?static?class?MyLogger
{
????static?readonly?LogWriter?_writer;
????//?日至記錄的類別
????const?string?ErrorCategory?=?"Error";
????const?string?DebugCategory?=?"Debug";
????//?文本文件的路徑
????const?string?LogFilePath?=?@"d:\\share\\messages.log";
????//?模版
????const?string?Template?=?"Timestamp:?{timestamp}{newline}"?+
????????????????????????????"Message:?{message}{newline}"?+
????????????????????????????"Category:?{category}{newline}"?+
????????????????????????????"Machine:?{machine}{newline}";
????static?MyLogger()
????{
????????//?實例化一個TextFormatter,使用前面定義的模版
????????TextFormatter?formatter?=?new?TextFormatter
????????????(Template);
????????//?實例化TraceListener,記錄到文本文件用FlatFileTraceListener
????????FlatFileTraceListener?logFileListener?=
????????????new?FlatFileTraceListener(LogFilePath,
???????????????????????????????????????"----------",
???????????????????????????????????????"----------",
???????????????????????????????????????formatter);
????????//?這里是TraceListener的集合,可以增加多個
????????LogSource?mainLogSource?=
????????????new?LogSource("MainLogSource",?SourceLevels.All);
????????mainLogSource.Listeners.Add(logFileListener);
????????IDictionary<string,?LogSource>?traceSources?=?new?Dictionary<string,?LogSource>();
????????traceSources.Add(ErrorCategory,?mainLogSource);
????????traceSources.Add(DebugCategory,?mainLogSource);
????????//?用來表示不記錄日志,這點需要注意一下
????????LogSource?nonExistantLogSource?=?new?LogSource("Empty");
????????//?創建一個類別過濾器
????????ICollection<string>?categoryfilters?=?new?List<string>();
????????categoryfilters.Add(DebugCategory);
????????CategoryFilter?categoryFilter?=?new?CategoryFilter("CategoryFilter",?categoryfilters,?CategoryFilterMode.AllowAllExceptDenied);
????????//?加入類別過濾器到集合中
????????ICollection<ILogFilter>?filters?=?new?List<ILogFilter>();
????????filters.Add(categoryFilter);
????????_writer?=?new?LogWriter(filters,
????????????????????????traceSources,
????????????????????????nonExistantLogSource,
????????????????????????nonExistantLogSource,
????????????????????????mainLogSource,
????????????????????????ErrorCategory,
????????????????????????false,
????????????????????????true);
????}
????/**////?<summary>
????///?記錄日志信息到Error,默認類別
????///?</summary>
????///?<param?name="message">日志信息</param>
????public?static?void?Write(string?message)
????{
????????Write(message,?ErrorCategory);
????}
????/**////?<summary>
????///?記錄日志信息到特定類別
????///?</summary>
????///?<param?name="message">日志信息</param>
????///?<param?name="category">類別</param>
????public?static?void?Write(string?message,?string?category)
????{
????????LogEntry?entry?=?new?LogEntry();
?
????????entry.Categories.Add(category);
????????entry.Message?=?message;
?
????????_writer.Write(entry);
????}
}
我們再來寫一個簡單的測試,注意上面的代碼中我們過濾掉了Debug類別的日志信息,這樣記錄到文本文件中的日志信息應該只有My Error一條:
public?partial?class?_Default?:?System.Web.UI.Page?{
????protected?void?Page_Load(object?sender,?EventArgs?e)
????{
????????MyLogger.Write("My?Error");
????????MyLogger.Write("My?Debug",?"Debug");
????}
}
文本文件中輸出的結果為:
----------
Timestamp: 2006-7-8 3:45:05
Message: My Error
Category: Error
Machine: RJ-097
----------
輸出的結果與我們設想的一致,使用編程的方法配置Logging Application Block簡單的就介紹到這里,你也可以使用這種方法來配置其他的應用程序塊。不過使用編程的方法來配置,失去了EL的靈活性,要知道EL的根本思想就是配置驅動,但是如果掌握了這些,也許你能夠更好的使用EL,在CodeProject上有人寫了一篇《Plug-in Manager for Logging - Configure MSEL2 On the fly》,有興趣的朋友不妨參考一下。
?
參考:
http://davidhayden.com/blog/dave/archive/2006/02/18/2805.aspx
http://geekswithblogs.net/akraus1/archive/2006/02/16/69784.aspx
轉載于:https://www.cnblogs.com/Terrylee/archive/2006/07/08/enterprise_library2_4.html
總結
以上是生活随笔為你收集整理的Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle 字段以逗号结尾的更新 数据
- 下一篇: i2c通信 msp430g2553_ms