Can you configure log4net in code instead of using a config file?

后端 未结 6 1208
日久生厌
日久生厌 2020-11-28 01:20

I understand why log4net uses app.config files for setting up logging - so you can easily change how information is logged without needing to recompile your cod

6条回答
  •  一生所求
    2020-11-28 01:35

    You can also escape XML completely, I wrote a sample with minimal programmatic configuration here.

    In a nutshell, here is what you need

    var tracer = new TraceAppender();
    var hierarchy = (Hierarchy)LogManager.GetRepository();
    hierarchy.Root.AddAppender(tracer);
    var patternLayout = new PatternLayout {ConversionPattern = "%m%n"};
    patternLayout.ActivateOptions();
    tracer.Layout = patternLayout;
    hierarchy.Configured = true;
    

提交回复
热议问题