练习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菜单命令,添加如下命名空间。

Enterprise Library 2.0 Hands On Lab 翻译(5):日志应用程序块(二)using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
Enterprise Library 2.0 Hands On Lab 翻译(5):日志应用程序块(二)
Enterprise Library 2.0 Hands On Lab 翻译(5):日志应用程序块(二)
using Microsoft.Practices.EnterpriseLibrary.Logging;
Enterprise Library 2.0 Hands On Lab 翻译(5):日志应用程序块(二)
Enterprise Library 2.0 Hands On Lab 翻译(5):日志应用程序块(二)
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
Enterprise Library 2.0 Hands On Lab 翻译(5):日志应用程序块(二)
Enterprise Library 2.0 Hands On Lab 翻译(5):日志应用程序块(二)
using Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;
Enterprise Library 2.0 Hands On Lab 翻译(5):日志应用程序块(二)
Enterprise Library 2.0 Hands On Lab 翻译(5):日志应用程序块(二)
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;

3.添加如下代码到ConsoleTraceListener类中。

Enterprise Library 2.0 Hands On Lab 翻译(5):日志应用程序块(二)[ConfigurationElementType(typeof(CustomTraceListenerData))]
Enterprise Library 2.0 Hands On Lab 翻译(5):日志应用程序块(二)
Enterprise Library 2.0 Hands On Lab 翻译(5):日志应用程序块(二)
public class ConsoleTraceListener : CustomTraceListener
}

注意这里需要继承于基类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 2.0 Hands On Lab 翻译(5):日志应用程序块(二)

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菜单命令。

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

3.设置属性FormatterText Formatter

4.选中Type属性,单击ellipses显示Type Selector对话框。

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

5.在程序集EnoughPI.Logging中选择ConsoleTraceListener类并单击OK按钮。

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

Type Selector列表中的类,来自于与Enterprise Library Configuration配置工具在同一目录下的程序集,它们继承于基类CustomTraceListener,并且有一个值为CustomTraceListenerData的特性ConfigurationElementType

6.选择Attributes属性并单击ellipses显示EditableKeyValue Collection Editor

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

7.单击Add按钮添加一对新的键值对:

Key = delimiter

Value = "---------------------------"

并单击OK按钮。

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

还记得我们在类ConsoleTraceListener中曾经有一个期望的参数名为delimiter,它将打印在每条日志项信息的开始位置。

8.选中Logging Application Block | Category Sources | General节点,选择Action | New | Trace Listener Reference菜单命令。

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

9.设置属性ReferencedTraceListenerCustom TraceListener

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

10.选择File | Save All菜单保存配置,并关闭Enterprise Library Configuration工具。

注意如果没有关闭Enterprise Library Configuration工具,在下次编译项目EnoughPI.Logging时编译完成后事件将失败。

 

第四步 查看TraceListener输出

1.选择项目EnoughPI,并选择Project | EnoughPI Properties…菜单命令,在项目属性对话框中选择Application设置Output typeConsole Application

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

2.选择File | Save All菜单保存。

3.选择Debug | Start Without Debugging菜单命令并运行应用程序,EnoughPI程序用来计算∏的精度。在NumericUpDown控件中输入你希望的精度并点击Calculate按钮。可以看到日志项显示在一个控制台窗口中。

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

完成后的解决方案代码如C:\Program Files\Microsoft Enterprise Library January 2006\labs\cs\Logging\exercises\ex02\end所示。

 

更多Enterprise Library的文章请参考《Enterprise Library系列文章

相关文章:

  • 2022-01-06
  • 2021-11-03
  • 2021-08-07
  • 2021-10-09
  • 2022-02-05
  • 2022-02-10
猜你喜欢
  • 2021-07-03
  • 2021-12-03
  • 2021-06-03
  • 2021-10-19
  • 2022-01-18
相关资源
相似解决方案