17

How to create the log file in appData folder. The path is C:\Users\MYNAME\AppData\Roaming\Project\My Project\Application. As soon as my project starts, the project folder is created on this path where this path is hard coded. How can I add my log file in this folder using log4net? I have made changes in the config file

<?xml version="1.0"?>
<configuration>
<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
    <appender name="Console" type="log4net.Appender.ConsoleAppender">
        <layout type="log4net.Layout.PatternLayout">
            <!-- Pattern to output the caller's file name and line    number -->
            <conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />
        </layout>
    </appender>
    <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">

        <file value="${APPDATA}\\Roaming\\Project\\My Project\\Application\\Log.txt"/>
        <appendToFile value="true" />
        <maximumFileSize value="100KB" />
        <maxSizeRollBackups value="10" />
        <layout type="log4net.Layout.PatternLayout">            
            <conversionPattern value="%level %thread %logger - %message%newline" />
        </layout>
    </appender>
    <root>
        <level value="ALL" />
        <appender-ref ref="Console" />
        <appender-ref ref="RollingFile" />
    </root>
</log4net>
<startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<system.serviceModel>
    <bindings />
    <client />
</system.serviceModel>
 </configuration>

This doesn't create any files in this folder. And all the permissions are granted being Administrator.

roopini n
  • 503
  • 2
  • 7
  • 29

2 Answers2

18

It seems that you are doing it the right way, however there are cases that it seems the casing of the variable makes a difference: An other question on the subject

You can try if: ${AppData} works

Community
  • 1
  • 1
Peter
  • 27,590
  • 8
  • 64
  • 84
  • Sorry i am able to create it. It should be . The ${AppData} automatically refers to Roaming. Is there a way to delete these files when the application is uninstalled? – roopini n Jun 10 '14 at 08:39
  • 1
    @roopinin only if you have a custom uninstaller as files created by log4net are not tracked by Windows Installer, as they are not created on install. – stuartd Jun 10 '14 at 08:46
  • @stuartd - I shall create a custom installer, thanks. – roopini n Jun 10 '14 at 09:11
  • If you want AppData\Local rather than Roaming use $(LocalAppData) instead – benjymous Apr 12 '16 at 12:36
  • 3
    @benjymous it works with `${LocalAppData}` (replace round brackets with curly brackets). And in case anyone needs some clarification: https://en.wikipedia.org/wiki/Bracket – itsho Mar 28 '17 at 11:44
  • FYI: Seems that in older versions of log4net you must use **capitalized** `${APPDATA}` – itsho Feb 28 '21 at 08:13
2

There are two ways to create a log file in Appdata folder using Log4net:

1) Creating file in path Appdata/local


<param name="File" value="${LOCALAPPDATA}\Logs\Jumbo4.txt"/>

Path log file created: 
C:\Users\<username>\AppData\Local\Logs

2)Creating file in path Appdata/roaming


<param name="File" value="${AppData}\Logs\Jumbo3.txt"/>

Path log file created: 
C:\Users\<username>\AppData\Roaming\Logs
Brijesh Ray
  • 779
  • 8
  • 15