程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 【日志應用程序塊】實驗2:創建和使用異步Trace Listener

【日志應用程序塊】實驗2:創建和使用異步Trace Listener

編輯:C#入門知識

To monitor how long the log entries take 監視日志條目的長度

    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記錄的時間。

     

            
         TextFormatter formatter =  TextFormatter(          xmlFormatterAttributes =      xmlFormatterAttributes[] =      xmlFormatterAttributes[] =      EnoughPI.Logging.Formatters.XmlFormatter xmlFormatter = 
                   
          eventLog =  EventLog(, ,  
          eventLogTraceListener =  
      
          flatFileTraceListener = 
                                              
          config =       
                      }

     

  1. 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的值。

     

     

     

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

    AddAsynchronousTraceListener method in the BuildProgrammaticConfig method in EntryPoint.cs to add the flatFileTraceListener to your configuration. 在EntryPoint.cs文件的BuildProgrammaticConfig方法中調用AddAsynchronousTraceListener方法來添加到你的配置信息中。
            
         TextFormatter formatter =  TextFormatter(
                 {severity}{newline}Title:{title}{newline}
      
         
          flatFileTraceListener =  
             FlatFileTraceListener(                     
         
          config =           (flatFileTraceListener); 
         config.IsTracingEnabled =       }

    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來異步的記錄消息。這在記錄大量日志消息到文件或數據庫時是非常有用的。

  1.  

     

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嘗試處理所有的異步緩沖區。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved