NLog不会在ASP.NET核心项目上创建日志文件

NLog不会在ASP.NET核心项目上创建日志文件,asp.net,nlog,Asp.net,Nlog,我不熟悉将NLog与ASP.NET Core结合使用,因此我遵循了以下指南: 我在项目目录的根目录下创建了以下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" auto

我不熟悉将NLog与ASP.NET Core结合使用,因此我遵循了以下指南:

我在项目目录的根目录下创建了以下
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"
      autoReload="true"
      internalLogLevel="Warn"
      internalLogFile="c:\temp\internal-nlog.txt">

  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
  </extensions>


  <!-- define various log targets -->
  <targets>
    <!-- write logs to file -->
    <target xsi:type="File" name="allfile" fileName="${basedir}\nlog-all-${shortdate}.log"
                 layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" />


    <target xsi:type="File" name="ownFile-web" fileName="${basedir}\nlog-own-${shortdate}.log"
             layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|  ${message} ${exception}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />

    <target xsi:type="Null" name="blackhole" />
  </targets>

  <rules>
    <!--All logs, including from Microsoft-->
    <logger name="*" minlevel="Trace" writeTo="allfile" />

    <!--Skip Microsoft logs and so log only own logs-->
    <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
    <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
  </rules>
</nlog>

在控制器内部,我调用这样一条线:

\u logger.LogInformation(“输入的自定义错误方法”)

在Visual Studio的输出窗口中返回以下内容:

CustomerMgmtAPI.Controllers.CustomerController:信息:输入的CustomerRange方法


但是,NLog从未创建实际日志文件。我想知道是否有人可以在这里指出NLog配置中的错误,因为我一直在查看ASP.NET核心项目的NLog文档,我自己找不到错误。

因此,实际解决问题的方法是从
NLog.config
文件中删除第一行:



我删除了这一行,一切都开始正常工作。我还注意到,当出现这一行时,Visual Studio给了我这些错误:

Invalid token 'Text' at root level of document.
Unexpected XML declaration. The XML declaration must be the first node in the document and no white space characters are allowed to appear before it.

在本例中,NLog教程似乎被破坏了,因为我刚刚从ASP.NET核心示例中接管了这个文件。我使用的是VS2017,因此可能与此版本的VS不兼容?

将NLog与ASP.NET核心一起使用的一个小秘密是,您可以像在ASP.NET和ASP.NET MVC中一样配置和创建日志。您只需像平常一样使用常规NLog Nuget包

只需在根目录中创建一个NLog.config,等等。您甚至不需要在配置或其他地方进行任何额外的配置就可以让它工作。您只需在类中引用NLog,然后使用LogManager创建一个记录器


这意味着Program.cs等中没有所有的wireup。

请检查内部日志@Julian I添加了以下行,但文件夹中没有创建内部日志文件:
internalLogFile=“C:\Users\User\Documents\visual studio 2017\Projects\AzureTableStorageShard\CustomerMgmtAPI\internal nlog.txt”
那么它可能找不到您的.config文件。它在您的根目录中吗?这很奇怪,因为我所有的nlog.config文件都有
。可能在