9

When I run my application locally it writes my log4net log to the location I've configured (i.e., <file value="${LOCALAPPDATA}\TEST\Logs\debug.log" />) without a problem. However, when I deploy my application via ClickOnce, the log file is not being written.

I know ClickOnce applications are limited in terms of where they can write to, but I was under the impression that LOCALAPPDATA (e.g., C:\Users\me\AppData\Local) was fair game.

Any ideas?

Rob Sobers
  • 20,737
  • 24
  • 82
  • 111

2 Answers2

15

With ClickOnce, the folder rules are somewhat different than for regular Windows apps. The data folder, where ClickOnce content files are deployed to, is located in c:\Users\me\Local Settings\Apps\2.0\Data. Beneath that folder are a couple of subfolder levels with rather cryptic identifiers.

So the actual data folder for a given ClickOnce app is best retrieved using the ApplicationDeployment class. You should also check the IsNetworkDeployed property to see if you're running in deployed mode:

if (ApplicationDeployment.IsNetworkDeployed)
{
    var dataDirectory = ApplicationDeployment.CurrentDeployment.DataDirectory;
    ...
}

Since the DataDirectory is decided by ClickOnce there is no way you can hardcode that path into your log4net config. My suggestion would be to modify the file paths programmaticaly at application startup.

foreach(var appender in LogManager.GetRepository().GetAppenders())
{
    var fileAppender = appender as FileAppender;
    if (fileAppender != null)
    {
        fileAppender.File = 
            fileAppender.File.Replace("${LOCALAPPDATA}", dataDirectory);
        fileAppender.ActivateOptions();
    }
}
Peter Lillevold
  • 33,668
  • 7
  • 97
  • 131
  • 1
    If this doesn't solve your problem, make sure the configuration file (e.g. log4net.config) isn't being deployed as a "Data File" – Pakman Mar 20 '13 at 20:37
  • 1
    Similar to what @Pakman said, you need to make sure the Build Action on the log4net.config file properties is set to Content (not None) in order for the log4net.config file to be deployed and read by your application. http://stackoverflow.com/a/16433824/602585 – deadlydog May 31 '16 at 20:25
3

Isolated Storage.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794