gpt4 book ai didi

c# - log4net.Filter.PropertyFilter 不工作

转载 作者:太空宇宙 更新时间:2023-11-03 14:57:59 25 4
gpt4 key购买 nike

我是使用 log4net 过滤器的 logenteries 新手,我已经尝试过剩余的过滤器,例如“level range,stringtomatch,LoggerMatchFilter”,它们都在工作很好,但是属性过滤器不起作用。我尝试了不同的解决方案,但它不起作用。

这是我的代码:

代码:

public class CustomFilter : FilterSkeleton
{
private bool acceptOnMatch;
private readonly IList<IFilter> filters = new List<IFilter>();

public override FilterDecision Decide(LoggingEvent loggingEvent)
{
if (loggingEvent == null)
throw new ArgumentNullException("loggingEvent");

foreach (IFilter filter in filters)
{
if (filter.Decide(loggingEvent) != FilterDecision.Accept)
return FilterDecision.Neutral; // one of the filter has failed
}

// All conditions are true
if (acceptOnMatch)
return FilterDecision.Accept;
else
return FilterDecision.Deny;
}

public IFilter Filter
{
set { filters.Add(value); }
}

public bool AcceptOnMatch
{
get { return acceptOnMatch; }
set { acceptOnMatch = value; }
}

}

我在log4net filter - how to write AND filter to ignore log messages中看到过这个解决方案但对我来说,过滤器不会添加到 interface 。我不知道是否直接添加了过滤器?在 loggingevent 中,我在 message 属性中传递数据,但我不知道如何将其添加到属性中,任何人都可以指导我,我不知道如何调用这个函数。

网络配置:

   <filter type="Namespace.CustomFilter, Assembly">
<filter type="log4net.Filter.PropertyFilter">
<key value="firstname" />
<stringToMatch value="1234" />
</filter>
<filter type="log4net.Filter.PropertyFilter">
<key value="address" />
<stringToMatch value="abcdf" />
</filter>
<acceptOnMatch value="false" />
</filter>

我遗漏了一些地方,请帮助我。过去几个小时我一直在挣扎。

更新:

    public string logFilter()
{
log4net.Config.XmlConfigurator.Configure();

ILog Log = LogManager.GetLogger(
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType
);

var ReferenceID = Guid.NewGuid().ToString();

var firstname = "1234";
var address ="abcdf";
var sno = "12";

Log.InfoFormat("firstname {0}-{1}", firstname, ReferenceID);

Log.InfoFormat("address {0}-{1}", address, ReferenceID);

Log.InfoFormat("sno {0}-{1}", sno, ReferenceID);

return ReferenceID;
}

此处 decide() 函数覆盖了 filterskelton 但属性过滤器未应用它能够从 webconfig 读取详细信息但它不能'过滤日志消息是否有我遗漏的内容。

提前致谢!

最佳答案

如果其中一个过滤器返回 FilterDecision.Neutral,则将记录日志消息。因此,您需要检查消息是否一起通过了所有过滤器

            public override FilterDecision Decide(LoggingEvent loggingEvent)
{
if (loggingEvent == null)
throw new ArgumentNullException("loggingEvent");

if (filters.All(x => x.Decide(loggingEvent) != FilterDecision.Accept))
{
return FilterDecision.Neutral;
}

// All conditions are true
if (acceptOnMatch)
return FilterDecision.Accept;
else
return FilterDecision.Deny;
}

更新:我的 log4net 配置看起来像这样:

 <filter type="MyApp.CustomFilter, MyApp">
<filter type="log4net.Filter.PropertyFilter">
<key value="firstname" />
<stringToMatch value="1234" />
</filter>
<filter type="log4net.Filter.PropertyFilter">
<key value="address" />
<stringToMatch value="abcdf" />
</filter>
<acceptOnMatch value="false"/>
</filter>
</appender>

示例如何检查日志记录

    Task.Run(() =>
{
log4net.ThreadContext.Properties["firstname"] = "123";
Log.Info("123");
});

Task.Run(() =>
{
log4net.ThreadContext.Properties["firstname"] = "1234";
Log.Info("1234");
});

Task.Run(() =>
{
log4net.ThreadContext.Properties["address"] = "abcdf";
Log.Info("abcdf");
});

关于c# - log4net.Filter.PropertyFilter 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47936958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com