本文演示Enterprise Library – Logging Application Block 日志管理模块的使用,以及如何创建和使用定制的TraceListener和LogFormatter。本文由http://blog.entlib.com 开源ASP.NET博客平台小组根据EntLib HOL手册编译提供,欢迎交流。
 
练习二:创建和使用定制的Trace Listener
本文练习如何创建一个定制的Trace Listener,用来传送格式化的日志记录到Console输出。接着将该Trace Listener应用到EnoughPI范例应用程序中,实时监控日志记录。
 
1. 首先打开\Enterprise Library 4.1 HOL\CS\Logging\exercises\ex02\begin目录下的EnoughPI.sln项目文件,该应用程序用来计算pi值。
 
2. 创建定制的Trace Listener
选择EnoughPI.Logging项目,添加对如下DLL的引用:
Microsoft.Practices.EnterpriseLibrary.Common.dll
Microsoft.Practices.EnterpriseLibrary.Logging.dll
 
选择TraceListeners\ConsoleTraceListener.cs 文件,添加对如下命名空间的引用:
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
 
添加如下代码到ConsoleTraceListener类中:
    [ConfigurationElementType(typeof(CustomTraceListenerData))]
    public class ConsoleTraceListener : CustomTraceListener
    {
        public ConsoleTraceListener() : base()
        {
        }
 
        public override void TraceData(TraceEventCache eventCache,string source, TraceEventType eventType, int id, object data)
        {
            if (data is LogEntry && this.Formatter != null)
            {
                this.WriteLine(this.Formatter.Format(data as LogEntry));
            }
            else
            {
                this.WriteLine(data.ToString());
            }
        }
 
        public override void Write(string message)
        {
            Console.Write(message);
        }
 
        public override void WriteLine(string message)
        {
            // Delimit each message
            Console.WriteLine((string)this.Attributes["delimiter"]);
 
            // Write formatted message
            Console.WriteLine(message);
        }
    }
CustomTraceListener基类要求重载2个抽象方法:Write 和 WriteLine。如需要格式化消息,则需要重载TraceDate方法。ConsoleTraceListener需要传入参数delimiter,该参数是listener一个配置项。记得要编译一下整个项目,这样可以生成CustomTraceListener的dll文件。
 
3. 应用定制的TraceListener
利用EntLib的配置管理工具对app.config进行配置。选择Application Block | Trace Listeners节点,添加一个新的Custom Trace Listener,并设置Formatter = Text Formatter。
 
 
接着选择Type属性,并点击按钮,弹出Type Selector对话框。
 

 

在弹出的对话框,点击Load from File按钮,找到EnoughPI.Logging 项目编译输出的文件夹,选择EnoughPI.Logging.dll文件。
 

 

选择EnoughPI.Logging 程序集中的ConsoleTraceListener类,然后点击OK按钮,如下图所示。

 
在属性窗口中,选择Attributes属性,点击相应的按钮,显示EditableKeyValue Collection Editor对话框,如下图所示。
 
 
在EditableKeyValue Collection Editor对话框,点击Add按钮,添加如下key/value:
Key = delimiter
Value = "---------------------------"
 

 
你应该还记得前面的ConsoleTraceListener代码中需要一个delimiter参数,在打印格式化的消息之前,先打印delimiter参数值。
 
下面选择Logging Application Block | Category Sources | General 节点,添加一个新的Trace Listener引用,并设置ReferencedTraceListener = Custom TraceListener(上一步导入的ConsoleTraceListener)。
 

 
现在别忘了保存配置文件。
 
4. 运行范例程序
运行范例程序,检查Visual Studio中Output窗口的输出,如下图所示。
 
 
下面改变项目的输出方式。选择EnoughPI项目,选择Project | EnoughPI Properties… 菜单项,在Application 选项页面中,设置Output type = Console Application。
 
 
再次运行范例程序,日志记录将显示在Console窗口,如下图所示。
 
 
 
现在,我们已经完成了创建和应用Custom Trace Listener。http://www.entlib.com专业ASP.NET电子商务平台小组,欢迎你继续访问下一节内容。
 
参考文档:
Logging Application Block Hands-On Labs for Enterprise Library
 
posted on 2010-09-15 09:56  vibratea  阅读(297)  评论(0编辑  收藏  举报