How to specify common application data folder for log4net?

后端 未结 6 1863
一生所求
一生所求 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 10:53

    We just use this:

    <param name="File" value="${ALLUSERSPROFILE}/Company/Product/Logs/error.log"/>
    

    It works great.


    This line can simply be inserted into your current appender configuration:

    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
       <param name="File" value="${ALLUSERSPROFILE}/Company/Product/Logs/error.log"/>
    </appender>
    
    0 讨论(0)
  • 2020-12-01 10:57

    This posting on the log4net mailinglist explains how you can define your own path replacement variables.

    0 讨论(0)
  • 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:

    <file type="log4net.Util.PatternString">
        <converter>
          <name value="folder" />
          <type value="MyAppName.SpecialFolderPatternConverter,MyAppName" />
        </converter>
        <conversionPattern value="%folder{CommonApplicationData}\\SomeOtherFolder\\log.txt" />
    </file>
    

    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.

    0 讨论(0)
  • 2020-12-01 11:06

    Complete and working solution - content of my Log4net.config file. In actual version of Log4Net there is no need for writing own pattern converter anymore

    <?xml version="1.0"?>
    <log4net>
      <root>
        <level value="INFO" />
        <appender-ref ref="LogFileAppender" />
      </root>
      <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
        <file type="log4net.Util.PatternString" value="%envFolderPath{CommonApplicationData}\\MyProject\\Logs\\log.txt" />
        <rollingStyle value="Size" />
        <maxSizeRollBackups value="10" />
        <maximumFileSize value="100MB" />
        <layout type="log4net.Layout.PatternLayout">
          <ConversionPattern type="log4net.Util.PatternString" value="%%-5p %%d{yyyy-MM-dd HH:mm:ss} - %%m%%n" />
        </layout>
      </appender>
    </log4net>
    
    0 讨论(0)
  • 2020-12-01 11:06

    In the current log4net version (2.0.8.0) you could simply use

    <file value="${ProgramData}\myFolder\LogFiles\" /> for C:\ProgramData

    ${LocalAppData} for C:\Users\user\AppData\Local\

    and ${AppData} for C:\Users\user\AppData\Roaming\

    0 讨论(0)
  • 2020-12-01 11:11

    Now (in 2018 FEB) as per log4net version 2.0.8.0.

    You can use without any converter to get Environment Variables as follows.

    <file type="log4net.Util.PatternString" value="%envFolderPath{CommonApplicationData}\\mylogfile.txt" />
    

    Refer: log4net.Util.PatternString class documentation for more details.

    0 讨论(0)
提交回复
热议问题