How to specify common application data folder for log4net?

后端 未结 6 1893
一生所求
一生所求 2020-12-01 10:13

I want log4net to write log files (using RollingFileAppender) to a subfolder of the common application data folder (e.g. C:\\Documents and Settings\\All Users\\Application D

6条回答
  •  长情又很酷
    2020-12-01 11:00

    Here's the full code from the log4net mailing list that pilif linked to:

    Basically the method is to implement a custom pattern converter for the log4net config file.

    First add this class to your project:

    public class SpecialFolderPatternConverter : log4net.Util.PatternConverter
    {
        override protected void Convert(System.IO.TextWriter writer, object state)
        {
            Environment.SpecialFolder specialFolder = (Environment.SpecialFolder)Enum.Parse(typeof(Environment.SpecialFolder), base.Option, true);
            writer.Write(Environment.GetFolderPath(specialFolder));
        }
    }
    

    Then set up the File parameter of your FileAppender as follows:

    
        
          
          
        
        
    
    

    Basically the %folder tells it to look at the converter called folder which points it to the SpecialFolderPatternConverter class. It then calls Convert on that class, passing in the CommonApplicationData (or whatever) enum value.

    Apparently in the next release of log4net (1.2.11) there will be a simpler method, as described here.

提交回复
热议问题