Enterprise Library 2.0 Hands On Lab 翻译(5):日志应用程序块(二)

练习2:创建并使用自定义的Trace Listener
在本练习中将创建一个自定义的Trace Listener,发送日志项到一个标准的控制台输出,并添加该Trace ListenerEnoughPI应用程序。
 
第一步
打开EnoughPI.sln项目,默认的安装路径应该为C:\Program Files\Microsoft Enterprise Library January 2006\labs\cs\Logging\exercises\ex02\begin,并编译。
 
第二步 创建自定义Trace Listener
1 .选择项目EnoughPI.Logging,选择Project | Add Reference…菜单命令,添加如下程序集,默认的位置应该为C:\Program Files\Microsoft Enterprise Library January 2006\bin
Microsoft.Practices.EnterpriseLibrary.Common.dll
Microsoft.Practices.EnterpriseLibrary.Logging.dll
2 .在解决方案管理器中选择TraceListeners\ConsoleTraceListener.cs文件,单击View | Code菜单命令,添加如下命名空间。
None.gif using  Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
None.gif
None.gif
using  Microsoft.Practices.EnterpriseLibrary.Logging;
None.gif
None.gif
using  Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
None.gif
None.gif
using  Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;
None.gif
None.gif
using  Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
3 .添加如下代码到ConsoleTraceListener类中。
None.gif [ConfigurationElementType( typeof (CustomTraceListenerData))]
None.gif
None.gif
public   class  ConsoleTraceListener : CustomTraceListener
ExpandedBlockStart.gif
{
InBlock.gif    
public ConsoleTraceListener()
InBlock.gif
InBlock.gif        : 
base()
ExpandedSubBlockStart.gif    
{
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
InBlock.gif    
public override void TraceData(TraceEventCache eventCache,
InBlock.gif
InBlock.gif        
string source, TraceEventType eventType, int id, object data)
InBlock.gif
ExpandedSubBlockStart.gif    
{
InBlock.gif        
if (data is LogEntry && this.Formatter != null)
InBlock.gif
ExpandedSubBlockStart.gif        
{
InBlock.gif            
this.WriteLine(this.Formatter.Format(data as LogEntry));
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
else
InBlock.gif
ExpandedSubBlockStart.gif        
{
InBlock.gif           
this.WriteLine(data.ToString());
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public override void Write(string message)
ExpandedSubBlockStart.gif    
{
InBlock.gif        Console.Write(message);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public override void WriteLine(string message)
InBlock.gif
ExpandedSubBlockStart.gif    
{
InBlock.gif        
// Delimit each message
InBlock.gif

InBlock.gif        Console.WriteLine((
string)this.Attributes["delimiter"]);
InBlock.gif
InBlock.gif        
// Write formatted message
InBlock.gif

InBlock.gif        Console.WriteLine(message);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}
注意这里需要继承于基类 CustomTraceListener,并且需要覆写基类中的两个抽象方法: Write(string message)和 WriteLine(string message),另外由于需要格式化日志信息,还需要覆写 TraceData方法。 ConsoleTraceListener 类有一个参数delimiter,作为Listener配置的一部分。
4 .拷贝输出程序集到Enterprise Library的安装bin目录下面。选择项目EnoughPI.Logging,选择Project | EnoughPI.Logging Properties 菜单命令,并选择Build Events项,添加如下命令到编译完成后事件中:
copy "$(TargetPath)" "..\..\..\..\..\..\..\..\..\bin"
Enterprise Library Configuration 工具将会自动加载相同目录下的程序集。
5 .选择File | Save All菜单保存。
6 .选择Build | Build Solution,编译整个解决方案。
 
第三步 使用自定义Trace Listener
1 .在解决方案管理器中选择项目EnoughPI的配置文件App.config文件,选择View | Open With…菜单命令,选择Enterprise Library Configuration并单击OK按钮。
2 .选中节点Logging Application Block | Trace Listeners,并选择Action | New | Custom Trace Listener菜单命令。
3 .设置属性FormatterText Formatter
4 .选中Type属性,单击ellipses显示Type Selector对话框。
5 .在程序集EnoughPI.Logging中选择ConsoleTraceListener类并单击OK按钮。
Type Selector列表中的类,来自于与Enterprise Library Configuration配置工具在同一目录下的程序集,它们继承于基类CustomTraceListener,并且有一个值为CustomTraceListenerData的特性ConfigurationElementType
6 .选择Attributes属性并单击ellipses显示EditableKeyValue Collection Editor
7 .单击Add按钮添加一对新的键值对:
Key = delimiter
Value = "---------------------------"
并单击OK按钮。
还记得我们在类ConsoleTraceListener中曾经有一个期望的参数名为delimiter,它将打印在每条日志项信息的开始位置。
8 .选中Logging Application Block | Category Sources | General节点,选择Action | New | Trace Listener Reference菜单命令。
9 .设置属性ReferencedTraceListenerCustom TraceListener
10 .选择File | Save All菜单保存配置,并关闭Enterprise Library Configuration工具。
注意如果没有关闭Enterprise Library Configuration工具,在下次编译项目EnoughPI.Logging时编译完成后事件将失败。
 
第四步 查看TraceListener输出
1 .选择项目EnoughPI,并选择Project | EnoughPI Properties…菜单命令,在项目属性对话框中选择Application设置Output typeConsole Application
2 .选择File | Save All菜单保存。
3 .选择Debug | Start Without Debugging菜单命令并运行应用程序,EnoughPI程序用来计算∏的精度。在NumericUpDown控件中输入你希望的精度并点击Calculate按钮。可以看到日志项显示在一个控制台窗口中。
完成后的解决方案代码如C:\Program Files\Microsoft Enterprise Library January 2006\labs\cs\Logging\exercises\ex02\end所示。
 
更多Enterprise Library的文章请参考《Enterprise Library系列文章













本文转自lihuijun51CTO博客,原文链接: http://blog.51cto.com/terrylee/67635  ,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值