如何使用企业库日志记录按类别路由日志文件

时间:2021-06-01 18:59:20

标签: c# enterprise-library-6

我无法弄清楚如何为不同的类别配置不同的日志文件。我正在使用 .net framework 4.5 和企业日志 6。我的要求非常简单。当我使用类别“传输”时,它应该写入一个名为“transfer.log”的日志文件。它应该将所有其他日志写入另一个名为“server.log”的文件。这是我的配置。使用此配置仅生成 server.log。没有“Transfer.log”的迹象,也没有错误或异常。我错过了什么

  <loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add name="Server Log" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging" 
           listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging" 
           fileName="E:\Logserver\Application\Logs\EventServer.log" footer="" formatter="Event Server Formatter" header="" 
           rollFileExistsBehavior="Increment" rollInterval="None" rollSizeKB="20000" timeStampPattern="dd-MM-yyyy" maxArchivedFiles="10" traceOutputOptions="None" filter="All" />

      <add name="Transfer Log" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging"
           listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging"
           fileName="E:\Application\Logs\EventTransfer.log" footer="" formatter="Event Transfer Formatter" header=""
           rollFileExistsBehavior="Increment" rollInterval="None" rollSizeKB="20000" timeStampPattern="dd-MM-yyyy" maxArchivedFiles="10" traceOutputOptions="None" filter="All" />
    </listeners>
    
    <formatters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging" 
           template="{timestamp(local)}, Category: {category}, Priority: {priority} Message: {message}" name="Event Server Formatter" />

      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging"
           template="{timestamp(local)}, {message}" name="Event Transfer Formatter" />
    </formatters>
    
    <logFilters>
    <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
      categoryFilterMode="AllowAllExceptDenied" name="Category Filter">
      <categoryFilters>
        <add name="Transfer" />
      </categoryFilters>
    </add>
      </logFilters>
    
    <categorySources>
      <add switchValue="All" autoFlush="True" name="Transfer">
        <listeners>
          <add name="Transfer Log" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category">
        <listeners>
          <add name="Event Server Log" />
        </listeners>
      </notProcessed>
      <errors switchValue="All" name="Logging Errors and Warnings">
        <listeners>
          <add name="Event Server Log" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>

0 个答案:

没有答案
相关问题