分享

【日志应用程序块】实验4:创建和使用自定义跟踪监听器

 昵称10504424 2014-02-13

Lab 4: Create and Use a Custom Trace Listener 创建和使用自定义跟踪监听器

In this lab, you will build a custom Trace Listener to send formatted log entries to the Console standard output. You will then add this new Trace Listener to the EnoughPI application and monitor the log entries in real-time. 在这个实验中,你将会创建一个自定义的跟踪监听器来将格式化后的日志条目发送到控制台标准输出。之后你将会添加这个新的跟踪监听器到EnoughPI程序并实时监视日志条目。

To begin this exercise, open the EnoughPI.sln file located in the ex04\begin folder. 打开ex04\begin文件夹中的EnoughPI.sln文件来开始这个练习。

To create a custom Trace Listener 创建一个自定义跟踪监听器

  1. using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

    using Microsoft.Practices.EnterpriseLibrary.Logging;

    using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;

    using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;

  2. Note: The base class is CustomTraceListener, which mandates that you override two abstract methods: Write(string message) and WriteLine(string message). However, to format the message we need to override the TraceData method.

    主意:基类是CustomTraceListener,其要求你重载两个抽象方法:Write(string message) WriteLine(string message)。不过格式化消息我们还需要重载TraceData方法。

    The ConsoleTraceListener is expecting a parameter, delimiter, as part of the listener configuration. ConsoleTraceListener类期望一个参数: delimiter,作为监听器配置的一部分。

  3. Select the Build | Build Solution menu command, to compile the complete solution. 选择 生成|生成解决方案 菜单命令,来编译整个解决方案。

To use a custom Trace Listener 使用自定义的跟踪监听器

  1. In EntryPoint.cs add your CustomTraceListener to your logging configuration in the BuildProgrammaticConfig method. 在EntryPoint.cs文件的方法BuildProgrammaticConfig中将你的自定义跟踪监听器添加到你的日志配置信息里。

    Because the LogSource named Category.General already exists, you can add another trace listener to it by referencing the LogSource by name, as above. 因为叫做Category.General 的LogSource已经存在,所以你可以通过引用LogSource的名字来添加另一个跟踪监听器到其中,如上面代码所示。

    Note: You will remember your ConsoleTraceListener is expecting a parameter named delimiter, which is printed before each formatted log entry is written to the console.

    注意:你还记得你的ConsoleTraceListener需要一个名为delimiter的参数,它将在在每个日志条目被输出到控制台之前打印出来。


To view the Trace Listener output查看跟踪监听器的输出

  1. Select the EnoughPI project. Select the Project | EnoughPI Properties… menu command, select the Application tab, and set Output type to Console Application. 选中EnoughPI项目,选择 项目|属性菜单命令,选择应用程序标签页,然后设置输出类型为控制台程序。

  2. Select the File | Save All menu command.选择 文件|保存全部 菜单命令。
  3. Select the Debug | Start Without Debugging menu command to run the application. Enter your desired precision and click the Calculate button. The log entries will be displayed in the application's console window, as you see here.选择 调试|开始执行(不调试)菜单命令来运行程序。输入你希望的精度并单击Calculate按钮。日志条目就会被显示在程序的控制台窗口里,就像你在这儿看到的一样:

To verify that you have completed the exercise correctly, you can use the solution provided in the ex04\e

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多