You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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 &amp; 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:记录错误及Critical
  • Warning:记录警告及Error、Critical
  • Information:记录信息及以上级别
  • Verbose:记录详细日志及以上
  • All:记录所有级别日志

内容的提问来源于stack exchange,提问作者Circle Hsiao

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 17:53:22