Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 企业库日志记录按类别(如某些值)进行筛选_C#_.net_Logging_Enterprise Library - Fatal编程技术网

C# 企业库日志记录按类别(如某些值)进行筛选

C# 企业库日志记录按类别(如某些值)进行筛选,c#,.net,logging,enterprise-library,C#,.net,Logging,Enterprise Library,我一直在寻找有关enterprise library 5.0日志记录和筛选的文档,但除了enterprise library CHM之外,找不到与我的场景相关的任何内容 我有几种类型的类别正在被记录,我想过滤日志,只挑选某些类别。 假设我有这些类别,一般,海龟,犀牛。A,犀牛。B,犀牛。C等等。很多犀牛 我只想选择像Rhino这样的类别 我相信我所做的是过滤任何东西,而不是一般的或乌龟,但是我宁愿尝试收集任何像犀牛的东西 即使是一些关于这方面的文档也会有所帮助 这是我当前的配置 <logg

我一直在寻找有关enterprise library 5.0日志记录和筛选的文档,但除了enterprise library CHM之外,找不到与我的场景相关的任何内容

我有几种类型的类别正在被记录,我想过滤日志,只挑选某些类别。 假设我有这些类别,一般,海龟,犀牛。A,犀牛。B,犀牛。C等等。很多犀牛

我只想选择像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之类的内容。\uuuuuu以确保日志中没有其他类别

<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>

企业库似乎不直接支持您的方案。您可能必须实现一个自定义日志过滤器

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

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