Search code examples
.netloggingservicelog4netprogram-files

How to get log4net to generate log files when running as non-administrator user?


A process running as a non-administrator user does not have rights to write to the program files folder. What is the best way to configure log4net to write to a location that a non-administrator user has rights to?

Ideally there would be:

  • A single configuration file or configuration from code would work for all versions of MS Windows supported by .NET.
  • Support for MS Windows services
  • Support for log4net version 1.2.0.30714 (we have to use this version)

Related questions:


Solution

  • Why can't you just configure log4net to write to a file in a folder to which you have proper access rights? You do that with a FileAppender:

    <appender name="FileAppender" type="log4net.Appender.FileAppender">
        <file value="c:/path/log-file.txt" />
        <appendToFile value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
        </layout>
    </appender>
    

    The above is taken from here. From the same page:

    <appender name="FileAppender" type="log4net.Appender.FileAppender">
        <file value="${TMP}\log-file.txt" />
        <appendToFile value="true" />
        <encoding value="unicodeFFFE" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
        </layout>
    </appender>
    

    You can use an environmental variable to set the path of the file.