Lab 2: Create and Use an Asynchronous Trace Listener 實驗2:創(chuàng)建和使用異步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監(jiān)聽器包裝來寫異步的將日志條目寫入磁盤文件。使用異步包裝來改變記錄日志條目的時間。操作將會更快的返回到程序,但是塊依然需要將日志條目寫到目的地。然后你會添加這個新的Trace Listener到EnoughPI程序來實時的監(jiān)控日志條目。
To begin this exercise, open the EnoughPI.sln file located in the ex02/begin folder. 要開始這個練習(xí),打開在ex02/begin文件夾中的EnoughPI.sln文件。
To monitor how long the log entries take 監(jiān)視日志條目的長度
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 }---------------------------------------- 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 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來異步的記錄消息。這在記錄大量日志消息到文件或數(shù)據(jù)庫時是非常有用的。
---------------------------------------- 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.
注意:如果程序在緩沖區(qū)被排空之前就被終止了,那么異步記錄消息可能會導(dǎo)致消息丟失。在關(guān)閉程序時處理LogWriter嘗試處理所有的異步緩沖區(qū)。

To verify that you have completed the exercise correctly, you can use the solution provided in the ex02/end folder. 要證實你是否正確的完成了練習(xí),你看以使用ex02/end文件夾中提供的解決方案。
新聞熱點
疑難解答