企业库日志记录按类别过滤类似值

时间:2013-05-10 21:17:00

标签: c# .net logging enterprise-library

我一直在寻找有关企业库5.0日志记录和过滤的文档,但在企业库CHM之外找不到与我的场景相关的任何内容。

我记录了几种类别的类别,我想过滤日志记录以仅选择某些类别。 说我有这些类别General,Turtle,Rhino.A,Rhino.B,Rhino.C等许多Rhinos。

我想只选择像Rhino这样的类别。 __

我相信我所做的是过滤掉任何不是General或Turtle的东西,但是我宁愿尝试拾取任何类似Rhino的东西。 __ 甚至一些关于此的文档也会有所帮助。

这是我当前的配置。

<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="false">
  <listeners>
    <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" source="Enterprise Library Logging" formatter="Text Formatter" log="" machineName="." traceOutputOptions="None" />
    <add name="Rolling Flat File Trace Listener"
         type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
         fileName="AppLog.log"
         formatter="Short Text Formatter" header="------------------------------------------------------" footer="------------------------------------------------------"
         rollInterval="Day" rollSizeKB="1024"
         timeStampPattern="yyyy-MM-dd"
         rollFileExistsBehavior="Increment"
         traceOutputOptions="LogicalOperationStack, DateTime" />
  </listeners>
  <formatters>
    <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" template="Timestamp: {timestamp(local)}{newline}App Domain: {localAppDomain}{newline}Machine: {localMachine}{newline}Title:{title}{newline}Severity: {severity}{tab}Message: {message}{newline}Category: {category}{newline}ProcessId: {localProcessId}{tab}Process Name: {localProcessName}{newline}Thread Name: {threadName}{tab}ThreadId:{win32ThreadId}&#xA;" name="Text Formatter" />
  </formatters>
  <logFilters>
    <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      categoryFilterMode="AllowAllExceptDenied" name="Category Filter">
      <categoryFilters>
        <add name="General" />
        <add name="Turtle" />
      </categoryFilters>
    </add>
  </logFilters>
  <categorySources>
    <add switchValue="All" name="General" />
    <add switchValue="All" name="Turtle" />
  </categorySources>
  <specialSources>
    <allEvents switchValue="All" name="All Events" />
    <notProcessed switchValue="All" name="Unprocessed Category">
      <listeners>
        <add name="Rolling Flat File Trace Listener" />
      </listeners>
    </notProcessed>
    <errors switchValue="All" name="Logging Errors &amp; Warnings">
      <listeners>
        <add name="Event Log Listener" />
      </listeners>
    </errors>
  </specialSources>
</loggingConfiguration>

过滤看起来如此,我正在捕捉除了General和Turtle之外的所有内容,但它看起来有点令人担忧,因为我宁愿只捕获像Rhino这样的东西。 __ 绝对确定日志很清楚其他类别。

<logFilters>
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
  categoryFilterMode="AllowAllExceptDenied" name="Category Filter">
  <categoryFilters>
    <add name="General" />
    <add name="Turtle" />
  </categoryFilters>
</add>
</logFilters>
<categorySources>
    <add switchValue="All" name="General" />
    <add switchValue="All" name="Turtle" />
</categorySources>

1 个答案:

答案 0 :(得分:3)

Enterprise Library似乎不直接支持您的方案。您可能必须实现自定义日志记录筛选器。

以下是它的外观(这是未经测试的代码):

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;
using System;
using System.Linq;

[ConfigurationElementType(typeof (CustomLogFilterData))]
public class RhinoFilter : LogFilter
{
    public RhinoFilter(string name)
        : base(name)
    {
    }

    public RhinoFilter(NameValueCollection settings) 
        : this("RhinoFilter")
    {
    }

    public override bool Filter(LogEntry log)
    {
        return log.Categories.Any(x => x.StartsWith("Rhino."));
    }
}