我是处理log4net
过滤器的logenteries的新手我已尝试过剩余过滤器,例如"等级范围,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中看到了这个解决方案,但对我来说,过滤器没有添加到接口。我是否无法直接添加过滤器?在 loggingevent 我传递消息属性中的数据,但我不知道如何添加到属性中,任何人都可以引导我,我不要&# 39;不知道如何调用此函数。
Webconfig:
<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;
}
这里决定()功能除了 filterskelton ,但属性过滤器没有应用它能够从 webconfig 读取详细信息,但它可以&# 39; t过滤日志消息是否有任何我错过的内容。
提前致谢!
答案 0 :(得分:0)
如果其中一个过滤器返回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");
});