Where can I write my log4net log file to under ClickOnce?

守給你的承諾、 提交于 2019-11-30 09:21:22

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();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!