Enterprise Library - Logging Application Block 学习手册(最新版) Part 1

it2022-05-05  71

本文演示Enterprise Library – Logging Application Block 日志管理模块的使用,以及如何创建和使用定制的TraceListener和LogFormatter。本文由 http://blog.entlib.com 开源ASP.NET博客平台小组根据EntLib HOL手册编译提供,欢迎交流。   练习一:为应用程序添加日志记录功能 本文练习如何使用Logging Application Block,通过 EntLib 配置工具来配置TraceListener。   1. 首先打开\Enterprise Library 4.1 HOL\CS\Logging\exercises\ex01\begin目录下的EnoughPI.sln项目文件,该应用程序用来计算pi值。可以直接运行应用程序,如下图所示:   2. 添加记录日志功能 选择EnoughPI项目,添加对logging dll的引用: Microsoft.Practices.EnterpriseLibrary.Logging.dll。 一般该dll在如下目录(根据安装选择的路径发生变化): C:\Program Files\Microsoft Enterprise Library 4.1 - October 2008\Bin   打开Calc\Calculator.cs 代码文件,添加命名空间的引用: using Microsoft.Practices.EnterpriseLibrary.Logging;   找到Calculator.cs代码文件中OnCalculated方法,添加如下代码:         protected void OnCalculated(CalculatedEventArgs args)         {             LogEntry log = new LogEntry();             log.Message = string.Format("Calculated PI to {0} digits", args.Digits);             log.Categories.Add(Category.General);             log.Priority = Priority.Normal;               Logger.Write(log);               if (Calculated != null)                 Calculated(this, args);         } 上述代码创建了一个新的LogEntry对象,设置相关参数值,并使用Logger.Write方法将entry对象写入到一个或多个TraceListener。这里使用到了Constants.cs代码中定义的一些常量,如Category和Priority。   下面找到Calculator.cs代码文件中的OnCalculating方法,添加如下log日志代码:         protected void OnCalculating(CalculatingEventArgs args)         {             Logger.Write(                 string.Format("Calculating next 9 digits from {0}", args.StartingAt),                 Category.General,                 Priority.Low             );               if (Calculating != null)                 Calculating(this, args);               if (args.Cancel == true)             {                 Logger.Write("Calculation cancelled by user!", Category.General, Priority.High);             }         } 上述代码使用了Logger.Write的重载方法,省略了创建一个LogEntry对象。   下面继续针对Calculator.cs文件中OnCalculatorException方法,记录异常信息,添加如下代码:         protected void OnCalculatorException(CalculatorExceptionEventArgs args)         {             if (!(args.Exception is ConfigurationErrorsException))             {                 Logger.Write(args.Exception, Category.General, Priority.High);             }               if (CalculatorException != null)                 CalculatorException(this, args);         } 注意:确保Exception类型不是ConfigurationErrorsException,否则因为没有正确配置Logging Application Block,将不能Logger来记录日志。一般情况下,我们使用Enterprise Library的Exception Handling Application Block来创建一致的异常处理策略。   3. 配置文件 使用EntLib 的Enterprise Library Configuration工具打开app.config配置文件,添加Logging Application Block配置项。     默认的Logging Application Block 配置项定义了一个General 的Category Source,General 目录有一个TraceListener的引用,名称为Formatted EventLog TraceListener。可以点击Category Sources节点,添加新的Category。   选择Logging Application Block | Trace Listeners | Formatted EventLog TraceListener 节点,设置Source 属性为 EnoughPI(注意:Source需要在Event Log中注册,在第一次使用时,Event Log Trace Listener自动注册一个新的Source,但是需要有管理员的权限,因此第一次需要以管理员的权限运行该应用程序)。     保存上述对app.config 文件的更新。   4. 运行范例程序 从事件日志(Event Log)查看生成的log记录。     5. 为应用程序添加跟踪功能 打开Calc\Calculator.cs 文件,添加tracing代码。         public string Calculate(int digits)         {             StringBuilder pi = new StringBuilder("3", digits + 2);             string result = null;               try             {                 if (digits > 0)                 {                     using (new Tracer(Category.Trace))                     {                         pi.Append(".");                         for (int i = 0; i < digits; i += 9)                         {                             CalculatingEventArgs args;                             args = new CalculatingEventArgs(pi.ToString(), i + 1);                             OnCalculating(args);                               // Break out if cancelled                             if (args.Cancel == true) break;                               // Calculate next 9 digits                             int nineDigits = NineDigitsOfPi.StartingAt(i + 1);                             int digitCount = Math.Min(digits - i, 9);                             string ds = string.Format("{0:D9}", nineDigits);                             pi.Append(ds.Substring(0, digitCount));                         }                     }                 }                   result = pi.ToString();                   // Tell the world I've finished!                 OnCalculated(new CalculatedEventArgs(result));             }             catch (Exception ex)             {                 // Tell the world I've crashed!                 OnCalculatorException(new CalculatorExceptionEventArgs(ex));             }               return result;         } 我们经常需要记录应用程序不同部分的运行时间,Logging Application Block的Tracing功能提供了对代码块记录执行时间的功能。Tracer对象在销毁时,会停止计时,并记录结束跟踪的消息。using 代码块可以确保在代码结束时,调用Tracer对象的Dispose() 方法。   再次使用EntLib的配置工具打开app.config配置文件,选择Logging Application Block节点,设置TracingEnabled属性为True(默认为True)。   选择Logging Application Block | Trace Listeners 节点,添加新的FlatFile TraceListener。     并设置Formatter属性为Text Formatter,如下图所示。     选择 Logging Application Block | Category Sources 节点,添加一个新的Category,并设置相应的属性。 Name = Trace SourceLevels = ActivityTracing     这里的Trace Category 在前面的代码中有使用到,ActivityTracing 限制仅仅在开始和结束时跟踪日志。   右键点击上一步创建的Trace Category,添加新的Trace Listener引用,并设置ReferencedTraceListener属性为 FlatFile TraceListener。其实,就是引用前一步创建的FlatFile TraceListener。     保存app.config配置文件。   6. 再次运行范例程序,检查trace.log文件。 在应用程序的bin目录下,可以找到trace.log文件,记录了trace 开始和结束的相关信息,如下图所示。     http://www.entlib.com专业ASP.NET电子商务平台小组,欢迎你继续访问下一节内容。   参考文档: Logging Application Block Hands-On Labs for Enterprise Library

转载于:https://www.cnblogs.com/vibratea/archive/2010/09/15/1826690.html

相关资源:各显卡算力对照表!

最新回复(0)