log4net 在运行时启用调试日志记录

发布于 2024-08-07 17:28:52 字数 1577 浏览 5 评论 0原文

我试图在没有 app.config 的情况下进行调试。我有以下代码:

public static class Logging
{
    private static ConsoleAppender GetConsoleAppender()
    {
        ConsoleAppender lAppender = new ConsoleAppender();
        lAppender.Name = "Console";
        lAppender.Layout = new log4net.Layout.PatternLayout("%date{dd-MM-yyyy HH:mm:ss,fff} %5level [%2thread] %message (%logger{1}:%line)%n");
        lAppender.Threshold = log4net.Core.Level.Error;
        lAppender.ActivateOptions();

        return lAppender;
    }


    private static FileAppender GetFileAppender()
    {
        FileAppender lAppender = new FileAppender();
        lAppender.Name = "File";
        lAppender.AppendToFile = true;
        lAppender.File = "C:\\Enum.log";
        lAppender.Layout = new
        log4net.Layout.PatternLayout("%date{dd-MM-yyyy HH:mm:ss,fff} %5level [%2thread] %message (%logger{1}:%line)%n");
        lAppender.Threshold = log4net.Core.Level.All;
        lAppender.ActivateOptions();



        return lAppender;
    }


    static Logging()
    {
        Logger root;
        root = ((Hierarchy)LogManager.GetRepository()).Root;
        root.AddAppender(GetConsoleAppender());
        root.AddAppender(GetFileAppender());
        root.Level = log4net.Core.Level.All;
        root.Repository.Configured = true;
    }
}

然后我像这样进行调用:

private static ILog lLog = LogManager.GetLogger(typeof(Logging));

这似乎有效,但是 lLog 的状态将所有 isDebugEnabled、isErrorEnabled 等设置为 false。所以我正在寻找一种在运行时修改这些的方法。

注意:我没有使用 app.config,因为这是从 MMC 加载的 dll。

Im trying to get debugging working without an app.config. I have the following code:

public static class Logging
{
    private static ConsoleAppender GetConsoleAppender()
    {
        ConsoleAppender lAppender = new ConsoleAppender();
        lAppender.Name = "Console";
        lAppender.Layout = new log4net.Layout.PatternLayout("%date{dd-MM-yyyy HH:mm:ss,fff} %5level [%2thread] %message (%logger{1}:%line)%n");
        lAppender.Threshold = log4net.Core.Level.Error;
        lAppender.ActivateOptions();

        return lAppender;
    }


    private static FileAppender GetFileAppender()
    {
        FileAppender lAppender = new FileAppender();
        lAppender.Name = "File";
        lAppender.AppendToFile = true;
        lAppender.File = "C:\\Enum.log";
        lAppender.Layout = new
        log4net.Layout.PatternLayout("%date{dd-MM-yyyy HH:mm:ss,fff} %5level [%2thread] %message (%logger{1}:%line)%n");
        lAppender.Threshold = log4net.Core.Level.All;
        lAppender.ActivateOptions();



        return lAppender;
    }


    static Logging()
    {
        Logger root;
        root = ((Hierarchy)LogManager.GetRepository()).Root;
        root.AddAppender(GetConsoleAppender());
        root.AddAppender(GetFileAppender());
        root.Level = log4net.Core.Level.All;
        root.Repository.Configured = true;
    }
}

Then later I make a call like so:

private static ILog lLog = LogManager.GetLogger(typeof(Logging));

Which seems to work, however the state of lLog has all of the isDebugEnabled, isErrorEnabled, etc set to false. So im looking for a way to modify these at runtime.

Note: I am not using an app.config becuase this is a dll being loaded from MMC.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别挽留 2024-08-14 17:28:52

如果您不调用静态方法之一或访问静态类的静态属性,则静态初始化程序将不会被执行。在您以某种方式访问​​类的静态成员之前,您的记录器将不会被初始化。

If you don't call one of the static methods or access a static property of your static class, your static initializer is not going to be executed. Until you make an access to the static members of the class in some way, your logger will not be initialized.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文