企业库日志按类别(如某些值)进行过滤

本文关键字:过滤 日志 企业库 | 更新日期: 2023-09-27 18:13:00

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

我有一些类型的类别被记录,我想过滤日志,只挑选某些类别。假设我有这些类别,一般,海龟,犀牛。犀牛。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>

企业库日志按类别(如某些值)进行过滤

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."));
    }
}