Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

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

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

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
401 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can programmatically change the Logging level of a log4net logger, but it's not obvious how to do so. I have some code that does this. Given this Logger:

private readonly log4net.ILog mylogger;

You have to do the following fancy footwork to set it to Debug:

((log4net.Repository.Hierarchy.Logger)mylogger.Logger).Level = log4net.Core.Level.Debug;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...