Changing the log level programmatically in log4net?

后端 未结 4 857
眼角桃花
眼角桃花 2020-12-14 09:51

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.

4条回答
  •  无人及你
    2020-12-14 10:17

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

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

    The log4net manual has lots of good configuration examples.

提交回复
热议问题