简体   繁体   English

NLog似乎没有登录ClickOnce环境,但在dev中工作

[英]NLog doesn't seem to be logging in ClickOnce environment but works in dev

I am performing logging in my application via NLog. 我正通过NLog在我的应用程序中执行日志记录。 I am trying to gather some logs to diagnose an odd issue I am having on only one machine. 我正在尝试收集一些日志来诊断我在一台机器上遇到的奇怪问题。 In my NLog.Config I have the following configuration: 在我的NLog.Config中,我有以下配置:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <targets>
    <target name="logfile" xsi:type="File" fileName="${specialfolder:dir=PollingLogs:file=log.txt:folder=MyDocuments}" />
  </targets>

  <rules>
    <logger name="*" minlevel="Debug" writeTo="logfile" />
  </rules>
</nlog>

When I run on my development machine I am getting a log.txt file created in my documents folder, with all logging happening correctly. 当我在我的开发机器上运行时,我得到一个在我的文档文件夹中创建的log.txt文件,所有日志记录都正确发生。 However, when I deploy the application out with ClickOnce however, no log.txt file is being created (that I can find at least). 但是,当我使用ClickOnce部署应用程序时,没有创建log.txt文件(我至少可以找到)。 No errors occur, and my app runs as normal, but nothing is happening. 没有错误发生,我的应用程序正常运行,但没有发生任何事情。

I am stumped at how to even debug this issue. 我很难理解如何调试这个问题。 Does anyone have any insight they can share to help me get NLog to work correctly in a clickonce environment? 有没有人有他们可以分享的任何见解,以帮助我让NLog在clickonce环境中正常工作?

As a reference, the NLog reference in my project is set to copy local. 作为参考,我的项目中的NLog引用设置为复制本地。

I know this is slightly late but there's another solution described here: Configure log4net or NLog with XML from code although it's answering another question. 我知道这有点晚了,但是这里有另一个解决方案: 使用代码中的XML配置log4net或NLog,尽管它正在回答另一个问题。

I had the same problem described by KallDrexx. 我遇到了KallDrexx描述的相同问题。 I'm not happy with multiple NLog config files floating about (multiple projects in the same solution) so created an NLogHelper class which has a method return the configuration: 我不满意多个NLog配置文件浮动(同一解决方案中的多个项目),因此创建了一个NLogHelper类,它有一个方法返回配置:

    public static XmlLoggingConfiguration SetNlogConfiguration()
    {
        var sr = new StringReader(NlogXmlConfigString1());
        var xr = XmlReader.Create(sr);
        var config = new XmlLoggingConfiguration(xr, null);
        return config;
    }

A separate method just returns the xml string, in my case: 在我的情况下,一个单独的方法只返回xml字符串:

    private static string NlogXmlConfigString1()
    {
        return  @"<nlog xmlns='http://www.nlog-project.org/schemas/NLog.xsd\'
                       xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance\'>
                  <targets>
                       <target name='logfile' xsi:type='File' fileName='${specialfolder:file=My_Logger.txt:folder=Desktop}' />
                  </targets>

                  <rules>
                    <logger name='*' minlevel='Info' writeTo='logfile' />
                  </rules>
                 </nlog>";
    }

Finally, I just set the configuration in the relevant class constructor: 最后,我只是在相关的类构造函数中设置配置:

    NLog.LogManager.Configuration = NlogHelper.Helper.SetNlogConfiguration();

Obviously it can be refactored and items like log file name can be passed as parameters if required. 显然它可以被重构,并且如果需要,可以将日志文件名之类的项作为参数传递。 The configuration xml can just be replaced with whatever your configuration is. 配置xml可以替换为您的配置。 Also seems to offer the possibility of easily specifying multiple configurations that are selected at run-time. 似乎还提供了轻松指定在运行时选择的多个配置的可能性。

I like it because I find it gives me greater control over the NLog configuration in code and during deployment. 我喜欢它,因为我发现它可以让我更好地控制代码和部署期间的NLog配置。

I found the answer to this issue when your NLog.config would not be copied to your program if installed using ClickOnce . 如果使用ClickOnce安装NLog.config不会复制到您的程序,我找到了这个问题的答案。

You need to select the NLog.config file in your Project using Solution Explorer and set its Properties as follows: 您需要使用Solution ExplorerProject选择NLog.config文件,并按如下所示设置其Properties

Build Action : Content 构建行动 :内容

Copy to Output Directory : Copy always 复制到输出目录 :始终复制

Now when you deploy using ClickOnce the files should be copied over as well! 现在,当您使用ClickOnce进行部署时,也应该复制文件! :) :)

To debug this use Sysinternal ProcessMon (free). 要调试这个,请使用Sysinternal ProcessMon (免费)。 It can show you among other things the files accesses tried/made of a process... 它可以向您显示尝试/制作过程的文件访问...

For this kind of Log I would recommend using ApplicationData or CommonApplicationData or LocalApplicationData from Environment.SpecialFolder and not MyDocuments ... 对于这种Log,我建议使用Environment.SpecialFolder ApplicationDataCommonApplicationDataLocalApplicationData而不是MyDocuments ...

I hit this issue with a project I was working on today. 我用今天正在处理的项目来解决这个问题。

The NLog.config file is getting copied to your build directories so it's fine when you run it from there, but the ClickOnce installers are not deploying the NLog.config file when the software is installed. NLog.config文件将被复制到您的构建目录,因此从那里运行它时很好, ClickOnce安装程序在安装软件时不会部署NLog.config文件。

This explains the behaviour you're seeing: when run from the installed copy, it just doesn't have any logging configuration. 这解释了您所看到的行为:从已安装的副本运行时,它只是没有任何日志记录配置。

The only solution I've found so far is to merge the logging config into your app.config file, because the exe.config file does get deployed by ClickOnce - I haven't had much joy trying to add the NLog.config to the installer. 到目前为止我发现的唯一解决方案是将日志配置合并到app.config文件中,因为exe.config文件确实由ClickOnce部署 - 我没有太多的乐趣尝试将NLog.config添加到安装程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM