技术开发 频道

Enterprise Library2.0(2):Logging Application Block学习


【IT168技术文档】

  Logging Application Block可以使开发人员在其应用程序中集成日志监测功能,看看随着2.0的推出给我们带来了哪些改变。

一.改进的地方

  1.Logging Application Block首先带来的是名称上的改变,在1.1下它的全称应该是Logging and Instrumentation Application Block,一般把它翻译为日志和检测应用程序块,而2.0下却完全变成了日志应用程序块。

  2.在1.1下,每个LogEntry只能被记录到一个Sink,而这种情况在2.0下已经不复存在,对于每个LogEntry对象,我们都可以通过Category指定很多的Sink。回忆一下在1.1时记录一个日志项:
LogEntry logEntry = new LogEntry(); logEntry.EventId = 100; logEntry.Priority = 2; logEntry.Message = "Informational message"; //只能设置一个 logEntry.Categorys = "UI Events"; Logger.Write(logEntry); 2.0下可以添加多次: LogEntry logEntry = new LogEntry(); logEntry.EventId = 100; logEntry.Priority = 2; logEntry.Message = "Informational message"; //设置多个Category logEntry.Categories.Add("Trace"); logEntry.Categories.Add("UI Events"); Logger.Write(logEntry); 3.可以在代码中查询哪些日志项将被过滤,例如: LogEntry logEntry = new LogEntry(); logEntry.Priority = 2; logEntry.Categories.Add("Trace"); logEntry.Categories.Add("UI Events"); if (Logger.ShouldLog(logEntry)) { // Event will be logged according to currently configured filters. } else { // Event will not be logged. }
  4.配置文件的改变。在1.1下关于Logging & Instrumentation Application Block的信息记录在loggingconfiguration.config文件中,2.0下所有的信息都放在了Web.config或App.config中,如:
<configuration> <configSections> <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging" /> </configSections> <loggingConfiguration tracingEnabled="true" defaultCategory="General"> <logFilters> <add name="Category" type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter, Microsoft.Practices.EnterpriseLibrary.Logging" categoryFilterMode="AllowAllExceptDenied"> <categoryFilters> <add name="UI Events" /> </categoryFilters> </add> <add name="Priority" type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.PriorityFilter, Microsoft.Practices.EnterpriseLibrary.Logging" minimumPriority="2" /> <add name="LogEnabled Filter" type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" enabled="true" /> </logFilters> </loggingConfiguration> </configuration>
0
相关文章