简体   繁体   English

log4net覆盖xml配置?

[英]log4net override xml configuration?

I am using log4net which is being configured via a config file. 我正在使用通过配置文件配置的log4net。 There is one case where I would like everything to be logged in debug (some code used during an upgrade). 在一种情况下,我希望所有内容都登录调试(升级过程中使用了一些代码)。 Is there anyway I can get the current logging level, set it to debug, run the specified code, then back to whatever it was before? 无论如何,我可以获得当前的日志记录级别,将其设置为调试,运行指定的代码,然后返回到以前的水平吗?

You can add a section in the config file specifically for DEBUG using 您可以使用以下方法在配置文件中专门为DEBUG添加部分:

 <logger name="MyApp.DebugLogging" additivity="false">
        <level value="DEBUG"/>
        <appender-ref ref="MainLog"/>
    </logger

Then in your code, you can access this logger using: 然后,在您的代码中,您可以使用以下方式访问此记录器:

 private static readonly ILog debuglog = log4net.LogManager.GetLogger("MyApp.DebugLogging");

And use the variable in your code block as 并在代码块中使用变量作为

 debuglog .Debug(ex);

The idea here is you should be able to create different loggers in your config file, then have flexibility of choosing which logger to use within your code blocks 这里的想法是您应该能够在配置文件中创建不同的记录器,然后可以灵活地选择在代码块中使用哪个记录器

Here's an idea: 这是一个主意:

Use log4net configuration file only. 仅使用log4net配置文件。 Set the "watch" flag to true, in order to watch it. 为了观看它,请将“ watch”标志设置为true。

Inside this configuration file, set a logging filter (in each appender). 在此配置文件中,设置日志记录过滤器 (在每个附加程序中)。 Set its min level to, say, Info or whatever. 将其最低级别设置为“ Info或其他Info See the Filters section in http://logging.apache.org/log4net/release/manual/configuration.html 请参阅http://logging.apache.org/log4net/release/manual/configuration.html中的“过滤器”部分

Then, before the upgrade phase, edit the configuration file and change the filter's min level to, say, Debug . 然后,在升级阶段之前,编辑配置文件,并将过滤器的最低级别更改为Debug

Once the upgrade is done, set the filter's min level back to its original value. 升级完成后,将过滤器的最低级别设置回其原始值。

这个问题讨论了Log4Net记录器的动态重新配置 ,它应该为您提供所需的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM