Lab 2: Create and Use an Asynchronous Trace Listener 实验2:创建和使用异步Trace Listener

In this lab, you will build an Asynchronous Trace Listener Wrapper to write log entries asynchronously to a disk file. Using the asynchronous wrapper changes the perceived time that it takes to log an entry. Control returns to the application faster, but the block still needs to write the log entry to its destination. You will then add this new Trace Listener to the EnoughPI application to monitor the log entries in real time. 在这个实验中,你将会建立一个异步Trace监听器包装来写异步的将日志条目写入磁盘文件。使用异步包装来改变记录日志条目的时间。操作将会更快的返回到程序,但是块依然需要将日志条目写到目的地。然后你会添加这个新的Trace Listener到EnoughPI程序来实时的监控日志条目。

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

To monitor how long the log entries take 监视日志条目的长度

  1. Comment out or remove the Event Log Trace Listener from the BuildProgrammaticConfig method in EntryPoint.cs so you are only keeping track of the time it takes to log using your Flat File Trace Listener.EntryPoint.cs 文件里BuildProgrammaticConfig方法中的Event Log Trace注释掉或删除掉,这样你就只记录了是用你的Flat File Trace Listener记录的时间。
     private static LoggingConfiguration BuildProgrammaticConfig()
    {
    // Formatter
    TextFormatter formatter = new TextFormatter(@"Timestamp:
    {timestamp(local)}{newline}Message:{message}{newline}Category:
    {category}{newline}Priority:{priority}{newline}EventId:
    {eventid}{newline}ActivityId:{property(ActivityId)}{newline}Severity:
    {severity}{newline}Title:{title}{newline}");
    var xmlFormatterAttributes = new NameValueCollection();
    xmlFormatterAttributes["prefix"] = "x";
    xmlFormatterAttributes["namespace"] = "EnoughPI/2.0";
    EnoughPI.Logging.Formatters.XmlFormatter xmlFormatter =
    new EnoughPI.Logging.Formatters.XmlFormatter(
    xmlFormatterAttributes);
    // Trace Listeners
    var eventLog = new EventLog("Application", ".", "EnoughPI");
    var eventLogTraceListener = new
    FormattedEventLogTraceListener(eventLog, formatter);
    var flatFileTraceListener =
    new FlatFileTraceListener(
    @"C:\Temp\trace.log",
    "----------------------------------------",
    "----------------------------------------",
    formatter);
    // Build Configuration
    var config = new LoggingConfiguration();
    config.AddLogSource(Category.General, SourceLevels.All, true).AddTraceListener(eventLogTraceListener);
    config.AddLogSource(
    Category.Trace,
    SourceLevels.ActivityTracing,
    true).AddTraceListener(flatFileTraceListener);
    return config;
    }
  2. Select the Debug | Start Without Debugging menu command to run the application. Enter a precision of at least 300 (this will make the time improvements more apparent) and click the Calculate button. The end of the Tracing logs in C:\Temp\trace.log will tell how long it took to calculate pi. 选择 调试|开始执行(不调试)菜单命令来运行程序。输入一个至少300(这样会使用时表现的多一些)然后单击Calculate按钮。在文件 C:\Temp\trace.log的末尾就会告诉你花了多长时间来计算PI的值。

    ----------------------------------------

    Timestamp: 7/22/2013 8:29:13 AM

    Message: End Trace: Activity '67ba73cf-502c-4c3d-bc04-c2ea11c7e88f' in

    method 'EnoughPI.Calc.Calculator.Calculate' at 1194567702026 ticks

    (elapsed time: 3.554 seconds)

    Category: Trace

    Priority: 5

    EventId: 1

    ActivityId: 67ba73cf-502c-4c3d-bc04-c2ea11c7e88f

    Severity: Stop

    Title:TracerExit

    ----------------------------------------

    ----------------------------------------

    Timestamp: 7/22/2013 8:29:13 AM

    Message: Calculated PI to 300 digits

    Category: General

    Priority: 2

    EventId: 100

    ActivityId: 00000000-0000-0000-0000-000000000000

    Severity: Information

    Title:

    ----------------------------------------

To use a trace listener asynchronously 使用异步Trace Listener

  1. Use the AddAsynchronousTraceListener method in the BuildProgrammaticConfig method in EntryPoint.cs to add the flatFileTraceListener to your configuration. 在EntryPoint.cs文件的BuildProgrammaticConfig方法中调用AddAsynchronousTraceListener方法来添加到你的配置信息中。

     private static LoggingConfiguration BuildProgrammaticConfig()
    {
    // Formatter
    TextFormatter formatter = new TextFormatter("Timestamp:
    {timestamp(local)}{newline}Message:
    {message}{newline}Category: {category}{newline}Priority:
    {priority}{newline}EventId: {eventid}{newline}ActivityId:
    {property(ActivityId)}{newline}Severity:
    {severity}{newline}Title:{title}{newline}"); // Trace Listeners
    var flatFileTraceListener = new
    FlatFileTraceListener(@"C:\Temp\trace.log",
    "----------------------------------------",
    "----------------------------------------",
    formatter); // Build Configuration
    var config = new LoggingConfiguration();
    config.AddLogSource(Category.Trace, SourceLevels.ActivityTracing,
    true).AddAsynchronousTraceListener(flatFileTraceListener);
    config.IsTracingEnabled = true;
    return config;
    }

    Wrapping the existing FlatFileTraceListener allows you to use that Trace Listener to log messages asynchronously. This will be most useful when writing large volumes of messages to a flat file or database. 包装已有的FlatFileTraceListener使你可以使用TraceListener来异步的记录消息。这在记录大量日志消息到文件或数据库时是非常有用的。

  2. View the output again. It should be significantly lower now. 再次查看输出文件,现在它看起来应该向下面的内容了。

    ----------------------------------------

    Timestamp: 7/22/2013 8:33:54 AM

    Message: End Trace: Activity '00c3f38c-233c-4d46-9958-19df15242634' in

    method 'EnoughPI.Calc.Calculator.Calculate' at 1195224522966 ticks

    (elapsed time: 0.912 seconds)

    Category: Trace

    Priority: 5

    EventId: 1

    ActivityId: 00000000-0000-0000-0000-000000000000

    Severity: Stop

    Title:TracerExit

    ----------------------------------------

    ----------------------------------------

    Timestamp: 7/22/2013 8:33:54 AM

    Message: Calculated PI to 300 digits

    Category: General

    Priority: 2

    EventId: 100

    ActivityId: 00000000-0000-0000-0000-000000000000

    Severity: Information

    Title:

Note: Logging messages asynchronously can lead to messages being lost if the application terminates before the buffer is drained. Disposing the LogWriter when shutting down the application attempts to flush all asynchronous buffers.

注意:如果程序在缓冲区被排空之前就被终止了,那么异步记录消息可能会导致消息丢失。在关闭程序时处理LogWriter尝试处理所有的异步缓冲区。

To verify that you have completed the exercise correctly, you can use the solution provided in the ex02\end folder. 要证实你是否正确的完成了练习,你看以使用ex02\end文件夹中提供的解决方案。

【企业库6】【日志应用程序块】实验2:创建和使用异步Trace Listener的更多相关文章

  1. 【翻译】【中英对照】【企业库6】动手实验 Hands-On Lab 日志应用程序块索引页

    Logging Application Block Hands-On Lab for Enterprise Library 企业库的日志应用程序块动手实验 This walkthrough shoul ...

  2. 使用Microsoft EnterpriseLibrary(微软企业库)日志组件把系统日志写入数据库和xml文件

    这里只是说明在项目中如何配置使用微软企业库的日志组件,对数据库方面的配置请参考其他资料. 1.在项目中添加Microsoft.Practices.EnterpriseLibrary.Data.dll. ...

  3. 微软企业库5.0 学习之路——第九步、使用PolicyInjection模块进行AOP—PART4——建立自定义Call Handler实现用户操作日志记录

    在前面的Part3中, 我介绍Policy Injection模块中内置的Call Handler的使用方法,今天则继续介绍Call Handler——Custom Call Handler,通过建立 ...

  4. 权限管理系统源码分析(ASP.NET MVC 4.0 + easyui + EF6.0 + MYSQL/MSSQLSERVER +微软企业库5.0+日志绶存)

    系统采用最先进技术开发: (ASP.NET MVC 4.0 + easyui + EF6.0 + MYSQL/MSSQLSERVER +微软企业库5.0+日志绶存) 大家可以加我QQ讨论 309159 ...

  5. [EntLib]微软企业库5.0 学习之路——第一步、基本入门

    话说在大学的时候帮老师做项目的时候就已经接触过企业库了但是当初一直没明白为什么要用这个,只觉得好麻烦啊,竟然有那么多的乱七八糟的配置(原来我不知道有配置工具可以进行配置,请原谅我的小白). 直到去年在 ...

  6. 基于微软企业库的AOP组件(含源码)

    软件开发,离不开对日志的操作.日志可以帮助我们查找和检测问题,比较传统的日志是在方法执行前或后,手动调用日志代码保存.但自从AOP出现后,我们就可以避免这种繁琐但又必须要实现的方式.本文是在微软企业库 ...

  7. Enterprise Library 企业库

    微软企业库,提供了一套日志,缓存等功能的库.可以通过NuGet安装.

  8. 在数据库访问项目中使用微软企业库Enterprise Library,实现多种数据库的支持

    在我们开发很多项目中,数据访问都是必不可少的,有的需要访问Oracle.SQLServer.Mysql这些常规的数据库,也有可能访问SQLite.Access,或者一些我们可能不常用的PostgreS ...

  9. Enterprise Library +Caliburn.Micro+WPF CM框架下使用企业库验证,验证某一个属性,整个页面的文本框都变红的原因

    我用的是CM这个框架做的WPF,在用企业库的验证的时候,我用标签的方式给一个属性加了不能为空的验证,但整个页面的所有控件的外面框都变红了.原因是CM框架的绑定方式是直接X:Name="你的属 ...

随机推荐

  1. R与数据分析旧笔记(五)数学分析基本

    R语言的各种分布函数 rnorm(n,mean=0,sd=1)#高斯(正态) rexp(n,rate=1)#指数 rgamma(n,shape,scale=1)#γ分布 rpois(n,lambda) ...

  2. nodejs:注册登录session出错以及连接Mongodb数据库时Error connecting to database解决方案

    (1)nodejs:注册登录session出错 解决办法: 在app.js 中将var MongoStore =  require(connect-mongo')改为var MongoStore =  ...

  3. FileInputStream(字节流)与fileReader(字符流) 的区别

    FileInputStream 类 1 ) FileInputStream 类介绍: 以字节为单位的流处理.字节序列:二进制数据.与编码无关,不存在乱码问题. FileInputStream 类的主要 ...

  4. K-Modes算法[聚类算法]

    聚类算法k-Modes的实现 <?php /* *Kmodes算法(聚类算法的实现) */ /* *获取簇的数目 */ //----------------------------------- ...

  5. codeforces 650B . Image Preview 二分

    题目链接 B. Image Preview time limit per test 1 second memory limit per test 256 megabytes input standar ...

  6. Mysql 笔记:

    1:可以查看information_schema.index_statistics 来查看索引的使用信息.还可以使用pt-index-usage 这个工具来分析日志再结合explain 来分析使用的索 ...

  7. InitParam与ContextParm的异同

    web.xml里面可以定义两种参数:(1)application范围内的参数,存放在servletcontext中,在web.xml中配置如下: xml 代码 <context-param> ...

  8. Javascript高级程序设计学习笔记一

    看完w3school的javascript的概念,有点基础,开始红皮书的路程,今晚总结前二章的心得. 第一章:javascript简介 重点是javascript的实现是由 ECMAScript(核心 ...

  9. The Highest Mark(01背包)

    The Highest Mark Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  10. nyist 202 红黑树(二叉树中序遍历)

    旋转对中序遍历没有影响,直接中序输出即可. #include <iostream> #include <cstdio> using namespace std; int n; ...