练习3 :创建并使用自定义LogFormatter<?XML:NAMESPACE PREFIX = O />
在本练习中将创建一个自定义的LogFormatter ,并在应用程序中使用它。
打开EnoughPI.sln 项目,默认的安装路径应该为C:\Program Files\Microsoft Enterprise Library January 2006\labs\cs\Logging\exercises\ex03\begin ,并编译。
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 格式,并且它期望接收两个参数
prefix 和
namespace 。
3
.选择Build | Build Solution 编译整个解决方案。
1
.在解决方案管理器中选择项目EnoughPI 的配置文件App.config 文件,选择View | Open With… 菜单命令,选择Enterprise Library Configuration 并单击OK 按钮。
2
.选中Logging Application Block | Formatters 节点,选择Action | New | Custom Formatter 菜单命令,并设置属性Name 的为Xml Formatter 。
<?XML:NAMESPACE PREFIX = V />
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 = namespace, Value = EnoughPI/2.0
还记得在类XmlFormatter 中期望接受的两个参数prefix 和namespace 。
7
.选择Logging Application Block | Trace Listeners | Custom TraceListener 节点,并设置属性Formatter 为Xml Formatter 。
8
.选择File | Save All 保存配置,并关闭Enterprise Library Configuration 工具。
9
.选择Debug | Start Without Debugging 菜单命令并运行应用程序,EnoughPI 程序用来计算∏的精度。在NumericUpDown 控件中输入你希望的精度并点击Calculate 按钮。可以看到日志项显示在一个控制台窗口中。
完成后的解决方案代码如C:\Program Files\Microsoft Enterprise Library January 2006\labs\cs\Logging\exercises\ex03\end 所示。
注意根据Hands On Lab 给出的时间建议,做完以上三个练习的时间应该为30 分钟。
更多Enterprise Library 的文章请参考《Enterprise Library系列文章 》
转载于:https://blog.51cto.com/terrylee/67636