简体   繁体   English

NLog引用项目没有日志输出

[英]NLog no log output from a referenced project

I am using NLog 4.5.11 in one Visual Studio 2017 solution. 我在一个Visual Studio 2017解决方案中使用NLog 4.5.11。 It has the nlog.config in the start up project, but the actual logging required is from another project, where NLog is referenced (but no nlog.config exists). 它在启动项目中具有nlog.config,但是实际所需的日志记录来自引用了NLog的另一个项目(但不存在nlog.config)。 Running this solution works fine, the NLog logs are being produced where I expect. 运行此解决方案效果很好,在我期望的位置生成了NLog日志。

The second VS solution uses (references) both the start up project and the one with the logging. 第二个VS解决方案同时使用(引用)启动项目和带有日志的项目。 One note on this project is that it is an Excel Add-on. 此项目的一个注意事项是它是一个Excel加载项。 When I run (debug from VS) this second solution, I do NOT get the NLog logging that should have been triggered via the referenced projects' code and NLog logging. 当我运行第二个解决方案(从VS调试)时,我没有得到应该通过引用的项目的代码和NLog日志记录触发的NLog日志记录。 I do not get any errors, exceptions, or error files etc. 我没有收到任何错误,异常或错误文件等。

I have tried also installing the NuGet packages for NLog to the second solution. 我也尝试将NLog的NuGet软件包安装到第二个解决方案。 I also tried adding a copy of the nlog.config to it. 我也尝试向其中添加nlog.config的副本。 I have looked in the build directory and the NLog dlls and config file are being copied there. 我查看了构建目录,并将NLog dll和配置文件复制到了那里。 I have also tried enabling the throwExceptions="true" and internalLogLevel="Trace". 我也尝试过启用throwExceptions =“ true”和internalLogLevel =“ Trace”。

I have been looking on SO and elsewhere, but I cannot find a solution or even how to debug it. 我一直在寻找SO和其他地方,但找不到解决方案,甚至如何调试它。 All that ever happens is simply no output, which is really frustrating. 曾经发生的一切仅仅是没有输出,这确实令人沮丧。

NLog.config 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"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="true"
      internalLogLevel="Trace" internalLogFile="c:\temp\nlog-internal.log">

  <variable name="appName" value="FSIS" />
  <variable name="logDir" value="c:\temp\Rbnz.Fsis.Logging" />
  <variable name="logDirArchive" value="c:\temp\Archive\Rbnz.Fsis.Logging" />

  <targets async="true">
    <target xsi:type="File"
            name="default"
            layout="${longdate} - ${level:uppercase=true}: ${message}${onexception:${newline}EXCEPTION\: ${exception:format=ToString}}"
            fileName="${logDir}\${shortdate}.log"
            keepFileOpen="false"
            archiveFileName="${logDirArchive}\${shortdate}.{##}.log"
            archiveNumbering="Sequence"
            archiveEvery="Day"
            maxArchiveFiles="30"
    />
  </targets>
  <rules>
    <logger name="*" writeTo="default" minlevel="Debug" />
  </rules>
</nlog>

Usage inside the code of the referenced project: 在引用项目的代码中的用法:

private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

and later on, inside a method: 然后在方法内部:

logger.Info("Start: CompileSeries()");
logger.Info("Done: CompileSeries()");

I expected that if NLog logging works for a project, then including that project as a reference in another project (in another solution), would trigger the same logging. 我希望,如果NLog日志记录适用于某个项目,那么将该项目作为另一个项目(在另一个解决方案中)的引用包括在内,将触发相同的日志记录。 Obviously I'm incorrect in this assumption. 显然我在这个假设中是错误的。

Based on what you wrote, you have a First Solution that has two projects--a startup project that configures a logger object. 根据您所写的内容,您有一个包含两个项目的First Solution-一个配置logger对象的启动项目。 That solution has a second project that references the startup project's logger object. 该解决方案还有一个引用启动项目的logger对象的项目。 Fine. 精细。

Now you have a Second Solution. 现在您有了第二个解决方案。 In this solution, you simply reference the First Solution's startup project assembly and the First Solution's second project. 在此解决方案中,您只需引用第一个解决方案的启动项目程序集和第一个解决方案的第二个项目。 Your Second Solution has its own project that's trying to access the logger exposed by the First Solution's referenced projects. 您的第二个解决方案有其自己的项目,该项目试图访问第一个解决方案的引用项目公开的logger

This issue is that your First Solution actually executes its startup project . 问题是您的第一个解决方案实际上执行其启动项目 Your Second Solution does not execute it--it simply references it . 您的第二个解决方案不执行它-只是引用它 Therefore, your logger object isn't getting properly initialized by NLog. 因此,您的logger对象没有被NLog正确初始化。

The solution (generically) is to ensure that your logger object is initialized in your Second Solution just as it's initialized in your First Solution. 解决方案(通常)是确保在第二个解决方案中初始化logger对象,就像在第一个解决方案中初始化一样。 If you want more specific guidance, show specifics as to how your First Solution's startup project initializes the logger object and I should be able to help you replicate that logic in the Second Solution. 如果您需要更具体的指导,请显示有关“第一个解决方案”的启动项目如何初始化logger对象的详细信息,我应该能够帮助您在“第二个解决方案”中复制该逻辑。

Your conclusion that you cannot log from another solution is incorrect. 您无法从其他解决方案登录的结论是不正确的。 It's all about proper object initialization. 这都是关于正确的对象初始化的。

I hope this helps. 我希望这有帮助。

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

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