Search code examples
c#wpfwindows-installerlog4netdesktop-application

Log4net in WPF desktop application log folder issues


When WPF desktop application installed on system , install folder not have proper permission to create log file . We not want to use event log as we need to register for Event Log event fist . That also making some issue .

How to create file and save log for installed Desktop application ?


Solution

  • Specify where the file is stored in the configuration file for log4net that you distribute with your app. You specify it in your AssemblyAttributes like this:

    [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net_myapp.config")]
    

    Then in the config file itself you list the appenders. Here, is an appender for a log file that uses the Windows TEMP folder (which anybody can write to safely) and prefixes each line with a timestamp, thread ID and log level

    <appender name="file" type="log4net.Appender.RollingFileAppender">
        <file type="log4net.Util.PatternString" value="%env{TEMP}/MyApp.log" />
        <appendToFile value="true" />
        <rollingStyle value="Size" />
        <maxSizeRollBackups value="5" />
        <maximumFileSize value="25MB" />
        <staticLogFileName value="true" />
        <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date{HH:mm:ss.fff} [%3thread] %5level - %message%newline" />
        </layout>
    </appender>