Enterprise Library 2.0 Hands On Lab 翻譯(6):日志應(yīng)用程序塊(三)
練習3:創(chuàng)建并使用自定義LogFormatter
在本練習中將創(chuàng)建一個自定義的LogFormatter,并在應(yīng)用程序中使用它。
第一步
打開EnoughPI.sln項目,默認的安裝路徑應(yīng)該為C:\Program Files\Microsoft Enterprise Library January 2006\labs\cs\Logging\exercises\ex03\begin,并編譯。
第二步 創(chuàng)建自定義LogFormatter
1.在解決方案管理器中選擇Formatters\XmlFormatter.cs文件,選擇View | Code菜單命令,添加如下命名空間。
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;2.添加如下代碼到XmlFormatter類中。
[ConfigurationElementType(typeof(CustomFormatterData))]
public class XmlFormatter : LogFormatter
{
private NameValueCollection Attributes = null;

public XmlFormatter(NameValueCollection attributes)
{
this.Attributes = attributes;
}

public override string Format(LogEntry log)
{
string prefix = this.Attributes["prefix"];
string ns = this.Attributes["namespace"];

using (StringWriter s = new StringWriter())
{
XmlTextWriter w = new XmlTextWriter(s);
w.Formatting = Formatting.Indented;
w.Indentation = 2;

w.WriteStartDocument(true);
w.WriteStartElement(prefix, "logEntry", ns);
w.WriteAttributeString("Priority", ns,
log.Priority.ToString(CultureInfo.InvariantCulture));
w.WriteElementString("Timestamp", ns, log.TimeStampString);
w.WriteElementString("Message", ns, log.Message);
w.WriteElementString("EventId", ns,
log.EventId.ToString(CultureInfo.InvariantCulture));
w.WriteElementString("Severity", ns, log.Severity.ToString());
w.WriteElementString("Title", ns, log.Title);
w.WriteElementString("Machine", ns, log.MachineName);
w.WriteElementString("AppDomain", ns, log.AppDomainName);
w.WriteElementString("ProcessId", ns, log.ProcessId);
w.WriteElementString("ProcessName", ns, log.ProcessName);
w.WriteElementString("Win32ThreadId", ns, log.Win32ThreadId);
w.WriteElementString("ThreadName", ns, log.ManagedThreadName);
w.WriteEndElement();
w.WriteEndDocument();

return s.ToString();
}
}
}日志項將被格式化為XML格式,并且它期望接收兩個參數(shù)prefix和namespace。
3.選擇Build | Build Solution編譯整個解決方案。
第三步 使用自定義LogFormatter
1.在解決方案管理器中選擇項目EnoughPI的配置文件App.config文件,選擇View | Open With…菜單命令,選擇Enterprise Library Configuration并單擊OK按鈕。
2.選中Logging Application Block | Formatters節(jié)點,選擇Action | New | Custom Formatter菜單命令,并設(shè)置屬性Name的為Xml Formatter。

3.選擇Type屬性,單擊ellipses顯示Type Selector對話框。

4.從程序集EnoughPI.Logging中選擇XmlFormatter類并單擊OK按鈕。

在Type Selector列表中的類,來自于與Enterprise Library Configuration配置工具在同一目錄下的程序集,它們繼承于基類LogFormatter,并且有一個值為CustomTraceListenerData的特性ConfigurationElementType。
5.選擇Attributes屬性并單擊ellipses顯示EditableKeyValue Collection Editor。

6.添加如下鍵值對
Key = prefix, Value = x
Key = namespace, Value = EnoughPI/2.0
并單擊OK按鈕。

還記得在類XmlFormatter中期望接受的兩個參數(shù)prefix和namespace。
7.選擇Logging Application Block | Trace Listeners | Custom TraceListener節(jié)點,并設(shè)置屬性Formatter為Xml Formatter。

8.選擇File | Save All保存配置,并關(guān)閉Enterprise Library Configuration工具。
9.選擇Debug | Start Without Debugging菜單命令并運行應(yīng)用程序,EnoughPI程序用來計算∏的精度。在NumericUpDown控件中輸入你希望的精度并點擊Calculate按鈕。可以看到日志項顯示在一個控制臺窗口中。

完成后的解決方案代碼如C:\Program Files\Microsoft Enterprise Library January 2006\labs\cs\Logging\exercises\ex03\end所示。
注意根據(jù)Hands On Lab給出的時間建議,做完以上三個練習的時間應(yīng)該為30分鐘。
Worktile,新一代簡單好用、體驗極致的團隊協(xié)同、項目管理工具,讓你和你的團隊隨時隨地一起工作。完全免費,現(xiàn)在就去了解一下吧。
https://worktile.com



浙公網(wǎng)安備 33010602011771號