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

首頁 > 學院 > 開發(fā)設計 > 正文

【企業(yè)庫6】【日志應用程序塊】實驗4:創(chuàng)建和使用自定義跟蹤監(jiān)聽器

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

【企業(yè)庫6】【日志應用程序塊】實驗4:創(chuàng)建和使用自定義跟蹤監(jiān)聽器

Lab 4: Create and Use a Custom Trace Listener 創(chuàng)建和使用自定義跟蹤監(jiān)聽器

In this lab, you will build a custom Trace Listener to send formatted log entries to the Console standard output. You will then add this new Trace Listener to the EnoughPI application and monitor the log entries in real-time. 在這個實驗中,你將會創(chuàng)建一個自定義的跟蹤監(jiān)聽器來將格式化后的日志條目發(fā)送到控制臺標準輸出。之后你將會添加這個新的跟蹤監(jiān)聽器到EnoughPI程序并實時監(jiān)視日志條目。

To begin this exercise, open the EnoughPI.sln file located in the ex04/begin folder. 打開ex04/begin文件夾中的EnoughPI.sln文件來開始這個練習。

To create a custom Trace Listener 創(chuàng)建一個自定義跟蹤監(jiān)聽器

  1. Select the TraceListeners/ConsoleTraceListener.cs file in the Solution Explorer. Select the View | Code menu command. Add the following namespaces:在解決方案資源管理器中選中 TraceListeners/ConsoleTraceListener.cs文件,然后選擇 視圖|代碼菜單命令。添加下面的命名空間:

    using Microsoft.PRactices.EnterpriseLibrary.Common.Configuration;

    using Microsoft.Practices.EnterpriseLibrary.Logging;

    using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;

    using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;

  2. Add the following highlighted code to the ConsoleTraceListener class. 添加高亮的代碼到ConsoleTraceListener類。
     1 [ConfigurationElementType(typeof(CustomTraceListenerData))] 2 public class ConsoleTraceListener : CustomTraceListener 3 { 4     public ConsoleTraceListener() 5         : base() 6     { 7     } 8  9     public ConsoleTraceListener(string del)10     {11         this.Attributes["delimiter"] = del;12     }13 14     public override void TraceData(TraceEventCache eventCache,15         string source, TraceEventType eventType, int id, object data)16     {17         if (data is LogEntry && this.Formatter != null)18         {19             this.WriteLine(this.Formatter.Format(data as LogEntry));20         }21         else22         {23             this.WriteLine(data.ToString());24         }25     }26 27     public override void Write(string message)28     {29         Console.Write(message);30     }31 32     public override void WriteLine(string message)33     {34         // Delimit each message35         Console.WriteLine((string)this.Attributes["delimiter"]);36         // Write formatted message37         Console.WriteLine(message);38     }39 }

    Note: The base class is CustomTraceListener, which mandates that you override two abstract methods: Write(string message) and WriteLine(string message). However, to format the message we need to override the TraceData method.

    主意:基類是CustomTraceListener,其要求你重載兩個抽象方法:Write(string message)WriteLine(string message)。不過格式化消息我們還需要重載TraceData方法。

    The ConsoleTraceListener is expecting a parameter, delimiter, as part of the listener configuration. ConsoleTraceListener類期望一個參數(shù): delimiter,作為監(jiān)聽器配置的一部分。

  3. Select the Build | Build Solution menu command, to compile the complete solution. 選擇 生成|生成解決方案 菜單命令,來編譯整個解決方案。

To use a custom Trace Listener 使用自定義的跟蹤監(jiān)聽器

  1. In EntryPoint.cs add your CustomTraceListener to your logging configuration in the BuildProgrammaticConfig method. 在EntryPoint.cs文件的方法BuildProgrammaticConfig中將你的自定義跟蹤監(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:  8     {property(ActivityId)}{newline}Severity:  9     {severity}{newline}Title:{title}{newline}"); 10 11     // Trace Listeners 12     var eventLog = new EventLog("Application", ".", "EnoughPI"); 13     var eventLogTraceListener = new 14         FormattedEventLogTraceListener(eventLog, formatter); 15     var flatFileTraceListener = new 16           FlatFileTraceListener( 17              @"C:/Temp/trace.log", 18              "----------------------------------------",  19              "----------------------------------------",20              formatter); 21 22     var customTraceListener =   23        new EnoughPI.Logging.TraceListeners.ConsoleTraceListener( 24            "-----------------------"); 25 26     // Build Configuration 27     var config = new LoggingConfiguration(); 28     config.AddLogSource( 29         Category.General,  30         SourceLevels.All,  31         true).AddTraceListener(eventLogTraceListener); 32     config.AddLogSource( 33         Category.Trace,  34         SourceLevels.ActivityTracing,  35         true).AddTraceListener(flatFileTraceListener); 36 37     config.LogSources[Category.General].AddTraceListener( 38         customTraceListener); 39     config.IsTracingEnabled = true; 40 41     return config; 42 }

    Because the LogSource named Category.General already exists, you can add another trace listener to it by referencing the LogSource by name, as above. 因為叫做Category.General 的LogSource已經(jīng)存在,所以你可以通過引用LogSource的名字來添加另一個跟蹤監(jiān)聽器到其中,如上面代碼所示。

    Note: You will remember your ConsoleTraceListener is expecting a parameter named delimiter, which is printed before each formatted log entry is written to the console.

    注意:你還記得你的ConsoleTraceListener需要一個名為delimiter的參數(shù),它將在在每個日志條目被輸出到控制臺之前打印出來。

  2. Set the Formatter tothe Text Formatter you created earlier. 設置格式器為你早先創(chuàng)建過的Text Formatter。

     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  10     // Trace Listeners 11     var eventLog = new EventLog("Application", ".", "EnoughPI"); 12     var eventLogTraceListener =  13         new FormattedEventLogTraceListener(eventLog, formatter); 14     var flatFileTraceListener =  15         new FlatFileTraceListener( 16            @"C:/Temp/trace.log", 17            "----------------------------------------", 18            "----------------------------------------",  19            formatter); 20     var customTraceListener =   21         new EnoughPI.Logging.TraceListeners.ConsoleTraceListener( 22             "----------------------"); 23     customTraceListener.Formatter = formatter; 24  25    // Build Configuration 26    var config = new LoggingConfiguration(); 27    config.AddLogSource( 28        Category.General, 29        SourceLevels.All,          30        true).AddTraceListener(eventLogTraceListener); 31    config.AddLogSource( 32        Category.Trace,  33        SourceLevels.ActivityTracing,  34        true).AddTraceListener(flatFileTraceListener); 35    config.LogSources[Category.General].AddTraceListener( 36     customTraceListener); 37              38  39    config.IsTracingEnabled = true; 40  41    return config; 42 }

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 齐齐哈尔市| 乡宁县| 巴彦淖尔市| 庆元县| 申扎县| 双牌县| 桐城市| 苏尼特右旗| 灌阳县| 集安市| 陵川县| 嵊州市| 莱芜市| 增城市| 三亚市| 壤塘县| 弥勒县| 建平县| 呼图壁县| 广平县| 博乐市| 璧山县| 泽州县| 侯马市| 特克斯县| 政和县| 秀山| 新竹市| 常德市| 宕昌县| 鹤岗市| 金山区| 海安县| 威海市| 平邑县| 连南| 双牌县| 遵义市| 车致| 桦南县| 凤山市|