Redirect log4net log in the User Directory

前端 未结 3 1041
轻奢々
轻奢々 2021-02-07 07:55

I have a program that uses log4net. It logs directly in the exe folder.

Now, when passing to station with Windows > XP (Vista, Seven) I observed that the logs are not a

相关标签:
3条回答
  • 2021-02-07 08:18

    Example of config.xml:

    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
        <file value="${USERPROFILE}\MyProgramName\Logs\rolling-log-" />
        <appendToFile value="true" />
        <maxSizeRollBackups value="5" />
        <maximumFileSize value="100KB" />
        <rollingStyle value="Date" />
        <datePattern value="yyyy-MM-dd.'log'" />
        <staticLogFileName value="false" />
        <layout type="log4net.Layout.PatternLayout">
            <header value="[Log opening]&#13;&#10;" />
            <footer value="[Log closing]&#13;&#10;" />
            <conversionPattern value="%date %-5level %logger - %message%newline" />
        </layout>
    </appender>
    

    remark the ${USERPROFILE} environment variable.

    0 讨论(0)
  • 2021-02-07 08:26

    Yes you can change the default log-file location. In the config change the file value setting. The following puts the file in the root of C:

    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
    <file value="c:\\log-file.txt"/>
    

    You can even use an Environment Variable, the following uses one called TMP:

    <appender name="FileAppender" type="log4net.Appender.FileAppender">
        <file value="${TMP}\log-file.txt" />
        <appendToFile value="true" />
    
    0 讨论(0)
  • 2021-02-07 08:29

    Instead of ${USERPROFILE} (see answer from moldovanu) you can use ${LOCALAPPDATA} or ${APPDATA}. This writes your log files to the the users AppData folder (local or roaming sub folder).

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