国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

【企業(yè)庫6】【日志應(yīng)用程序塊】實驗2:創(chuàng)建和使用異步Trace Listener

2019-11-17 03:22:11
字體:
供稿:網(wǎng)友

【企業(yè)庫6】【日志應(yīng)用程序塊】實驗2:創(chuàng)建和使用異步Trace Listener

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. 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. 選擇 調(diào)試|開始執(zhí)行(不調(diào)試)菜單命令來運(yùn)行程序。輸入一個至少300(這樣會使用時表現(xiàn)的多一些)然后單擊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方法中調(diào)用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來異步的記錄消息。這在記錄大量日志消息到文件或數(shù)據(jù)庫時是非常有用的。

  2. View the output again. It should be significantly lower now. 再次查看輸出文件,現(xiàn)在它看起來應(yīng)該向下面的內(nèi)容了。

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

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


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 漯河市| 江阴市| 奉贤区| 民和| 洞口县| 长岛县| 天津市| 武宣县| 岫岩| 萨嘎县| 乌拉特前旗| 通河县| 邵阳市| 绍兴市| 鄂伦春自治旗| 洪江市| 柘城县| 耒阳市| 上饶县| 孝义市| 寻乌县| 曲松县| 蒙阴县| 长海县| 义乌市| 南汇区| 营口市| 博客| 安国市| 台湾省| 灵宝市| 金湖县| 安西县| 关岭| 中宁县| 张家界市| 理塘县| 正定县| 庆元县| 江山市| 靖西县|