1

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

我记录了几种类型的类别,我想过滤日志以仅选择某些类别。假设我有这些类别 General、Turtle、Rhino.A、Rhino.B、Rhino.C 等很多犀牛。

我只想选择 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>
4

1 回答 1

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."));
    }
}
于 2013-05-20T13:44:37.450 回答