【企业库6】【日志应用程序块】实验2:创建和使用异步Trace Listener

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记录的时间。

     

     1 private static LoggingConfiguration BuildProgrammaticConfig() 
     2 { 
     3     // Formatter 
     4     TextFormatter formatter = new TextFormatter(@"Timestamp: 
     5         {timestamp(local)}{newline}Message:{message}{newline}Category: 
     6         {category}{newline}Priority:{priority}{newline}EventId: 
     7         {eventid}{newline}ActivityId:{property(ActivityId)}{newline}Severity: 
     8         {severity}{newline}Title:{title}{newline}"); 
     9     var xmlFormatterAttributes = new NameValueCollection(); 
    10     xmlFormatterAttributes["prefix"] = "x"; 
    11     xmlFormatterAttributes["namespace"] = "EnoughPI/2.0"; 
    12     EnoughPI.Logging.Formatters.XmlFormatter xmlFormatter = 
    13         new EnoughPI.Logging.Formatters.XmlFormatter( 
    14         xmlFormatterAttributes); 
    15     // Trace Listeners 
    16     var eventLog = new EventLog("Application", ".", "EnoughPI"); 
    17     var eventLogTraceListener = new 
    18         FormattedEventLogTraceListener(eventLog, formatter); 
    19     var flatFileTraceListener = 
    20         new FlatFileTraceListener( 
    21         @"C:\Temp\trace.log", 
    22         "----------------------------------------", 
    23         "----------------------------------------", 
    24         formatter); 
    25     // Build Configuration 
    26     var config = new LoggingConfiguration(); 
    27     config.AddLogSource(Category.General, SourceLevels.All,  true).AddTraceListener(eventLogTraceListener); 
    28     config.AddLogSource( 
    29         Category.Trace, 
    30         SourceLevels.ActivityTracing, 
    31         true).AddTraceListener(flatFileTraceListener); 
    32     return config; 
    33 }

     

  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方法来添加到你的配置信息中。
     1 private static LoggingConfiguration BuildProgrammaticConfig()
     2 {
     3     // Formatter 
     4     TextFormatter formatter = new TextFormatter("Timestamp:   
     5         {timestamp(local)}{newline}Message: 
     6         {message}{newline}Category: {category}{newline}Priority:  
     7         {priority}{newline}EventId: {eventid}{newline}ActivityId:  
     8         {property(ActivityId)}{newline}Severity:   
     9         {severity}{newline}Title:{title}{newline}"); 
    10  
    11     // Trace Listeners 
    12     var flatFileTraceListener = new 
    13         FlatFileTraceListener(@"C:\Temp\trace.log", 
    14         "----------------------------------------",
    15         "----------------------------------------",
    16         formatter); 
    17  
    18     // Build Configuration 
    19     var config = new LoggingConfiguration(); 
    20     config.AddLogSource(Category.Trace, SourceLevels.ActivityTracing, 
    21         true).AddAsynchronousTraceListener(flatFileTraceListener); 
    22     config.IsTracingEnabled = true;
    23     return config;
    24 }

    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文件夹中提供的解决方案。

posted on 2014-02-10 11:11  Arnu  阅读(492)  评论(0编辑  收藏  举报