改变log4net的日志级别programmaticaly? [英] Changing the log level programmaticaly in log4net?

查看:136
本文介绍了改变log4net的日志级别programmaticaly?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法来设置log4net的编程方式的日志级别?我会认为会有这将让你这样做的属性,但我似乎无法找到一个。

Is there a way to set the log level in log4net programmatically? I'd assumed that there would be a property that would let you do this, but I can't seem to find one.

我想要做的是有一个配置选项进入调试模式。这将导致额外的日志记录。

What I want to do is have a configurable option to enter debug mode. which would cause extra logging.

我使用一个单独的log4net的配置XML文件。此刻,我拿出解决方案如下:

I'm using a separate log4net configuration xml file. At the moment the solutions I've come up with are as follows:


  1. 使用DOM编辑日志文件,然后调用XMLConfigurator日志文件配置为每个文件。

  1. Edit the log file using the dom and then call XMLConfigurator to configure the log file as per the file.

有两个日志配置文件,并在不断变化的通话XML配置选择使用相应的日志配置文件

Have two log configuration files, and on the option changing call xml Configurator to use the appropriate log configuration file.

我倾向于2,有什么原因,这不会工作?

I'm leaning towards 2, is there any reason this wont work?

推荐答案

这是我编程方式配置log4net的方式:

This is the way I'm configuring log4net programmatically:

//declared as a class variable   
private static readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

 PatternLayout layout = new PatternLayout(@"%date %-5level %message%newline");
    RollingFileAppender appender = new RollingFileAppender();
    appender.Layout = layout;
    appender.File = "c:\log.txt";
    appender.AppendToFile = true;
    appender.MaximumFileSize = "1MB";
    appender.MaxSizeRollBackups = 0; //no backup files
    appender.Threshold = Level.Error;
    appender.ActivateOptions();                       
    log4net.Config.BasicConfigurator.Configure(appender);



我觉得追加程序的阈值属性是你在寻找什么,它会控制日志记录的appender是什么水平将输出。

I think the Appender's Threshold property is what you looking for, it will control what levels of logging the appender will output.

log4net的的手动有很多很好的配置实例。

The log4net manual has lots of good configuration examples.

这篇关于改变log4net的日志级别programmaticaly?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆