这是我尝试在不使用xml的情况下设置log4net:
public class Logger
{
public static void Setup()
{
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
PatternLayout patternLayout = new PatternLayout();
patternLayout.ConversionPattern = "%date [%thread] %-5level %logger - %message%newline";
patternLayout.ActivateOptions();
RollingFileAppender roller = new RollingFileAppender();
roller.AppendToFile = false;
roller.File = @"Logs\EventLog.txt";
roller.Layout = patternLayout;
roller.MaxSizeRollBackups = 5;
roller.MaximumFileSize = "1GB";
roller.RollingStyle = RollingFileAppender.RollingMode.Size;
roller.StaticLogFileName = true;
roller.ActivateOptions();
hierarchy.Root.AddAppender(roller);
MemoryAppender memory = new MemoryAppender();
memory.ActivateOptions();
hierarchy.Root.AddAppender(memory);
hierarchy.Root.Level = Level.Info;
hierarchy.Configured = true;
}
}
现在我需要转换这个xml代码:
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="error"/>
</filter>
<filter type="log4net.Filter.DenyAllFilter"/>
进入我的C#代码。
但问题是我不知道如何使用过滤器。
我可以创建一个过滤器:log4net.Filter.IFilter filter;
但filter
不提供these个选项......
你能帮忙把过滤器放到我的代码中吗?
答案 0 :(得分:6)
尝试:
...
RollingFileAppender roller = new RollingFileAppender();
roller.AppendToFile = false;
roller.File = @"Logs\EventLog.txt";
roller.Layout = patternLayout;
roller.MaxSizeRollBackups = 5;
roller.MaximumFileSize = "1GB";
roller.RollingStyle = RollingFileAppender.RollingMode.Size;
roller.StaticLogFileName = true;
var filter = new log4net.Filter.StringMatchFilter
{
StringToMatch = "error",
AcceptOnMatch = true
}
roller.AddFilter(filter);
var filterDeny = new log4net.Filter.DenyAllFilter();
roller.AddFilter(filterDeny);
...