打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

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

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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
引用 怎么把别人博客上的好东西搬到自己的博客上
Flex Builder教程
在 .NET 程序中启用调试信息输出
适用于每个LabVIEW开发者的巧妙调试技巧
20应用程序,让你更健康
Asp.Net Trace 全功略
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服