gpt4 book ai didi

c# - 如何以编程方式向 log4net 添加过滤器?

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

这是我在不使用 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选项...

你能帮忙把过滤器插入我的代码吗?

最佳答案

尝试:

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

关于c# - 如何以编程方式向 log4net 添加过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34700398/

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