我是在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中看到了此解决方案 - 如何编写和过滤以忽略日志消息,但对我而言,过滤器不添加到接口。我没有明白是否直接添加过滤器?在 loggingevent 中,我正在传递消息> Message 属性中的数据,但是我不知道如何将其添加到属性中,请指导我,我不知道如何调用此功能。
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;
}
shore decile()函数从 filterskelton 中过度过度,但是属性过滤器不应用于能够从 webconfig 中读取详细信息,但是它可以't过滤日志消息有我错过的任何东西。
预先感谢!
如果其中一个过滤器返回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");
});