如何在app.config中设置EnterpriseLibrary.Logging的日志严重级别?
问题
我负责维护一个开发已久的Windows Service,原作者早已离职。我希望将日志严重级别从Information提升至Warning以减少事件日志记录,但在app.config中未找到可限制日志写入严重级别的配置项,附上当前配置代码,恳请使用过该方案的开发者指导配置方法。
当前app.config日志配置
<configSections> <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true"/> <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <section name="CompanyName0PBR_Service.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/> </sectionGroup> </configSections> <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General"> <listeners> <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" source="PBRProcessing" formatter="Text Formatter" log="CompanyName0PKPBR_Service" machineName="." traceOutputOptions="None" /> </listeners> <formatters> <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" template="Message: {message}{newline}" name="Text Formatter"/> </formatters> <categorySources> <add switchValue="All" name="General"> <listeners> <add name="Event Log Listener"/> </listeners> </add> </categorySources> <specialSources> <allEvents switchValue="Off" name="All Events"/> <notProcessed switchValue="Off" name="Unprocessed Category"/> <errors switchValue="Off" name="Logging Errors & Warnings"> <listeners> <add name="Event Log Listener"/> </listeners> </errors> </specialSources> </loggingConfiguration>
解决方案
针对Enterprise Library 5.0的日志配置,你需要修改categorySources节点下对应分类的switchValue属性:
- 当前你的
General分类的switchValue是All,这意味着会记录所有级别的日志 - 将其修改为
Warning,这样就只会记录Warning及更高级别的日志(包含Error、Critical等),从而过滤掉Information级别的日志
修改后的categorySources节点如下:
<categorySources> <add switchValue="Warning" name="General"> <listeners> <add name="Event Log Listener"/> </listeners> </add> </categorySources>
补充说明
switchValue的可选值按级别从高到低为:
Off:完全关闭日志Critical:仅记录严重错误Error:记录错误及CriticalWarning:记录警告及Error、CriticalInformation:记录信息及以上级别Verbose:记录详细日志及以上All:记录所有级别日志
内容的提问来源于stack exchange,提问作者Circle Hsiao
相关产品推荐
相关产品推荐

