NLOG不会在ASP.NET Core Project上创建日志文件



我是与ASP.NET Core一起使用NLOG的新手,因此我在此处遵循了指南:https://github.com/nlog/nlog.web/wiki/getting-with-with-asp.net-core-(project.json(

我在项目目录的根部创建了以下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:tempinternal-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>

在控制器中,我称之为这样的行:

_logger.LogInformation("Entered CustomerRange method");

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

CustomerMgmtAPI.Controllers.CustomerController:Information: Entered CustomerRange method

但是,实际日志文件从未由NLOG创建。我想知道是否有人在此处指出NLOG配置中的错误,因为我一直在审查ASP.NET Core Project的NLOG文档,并且我自己找不到错误。

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

<?xml version="1.0" encoding="utf-8" ?>

我删除了这条线,一切都开始按预期工作。我还注意到,视觉工作室在出现那条线时给了我这些错误:

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 Core的示例中接管了此文件。我正在使用VS2017,所以也许与此版本的VS?

有不相容性

与ASP.NET Core一起使用NLOG的肮脏小秘密是您可以像ASP.NET和ASP.NET MVC中一样配置和创建日志。您只会像往常一样使用常规的nlog nuget软件包。

只需在您的根部创建一个nlog.config。您甚至不必在配置或其他地方进行任何额外的配置即可使其正常工作。您只需在课堂中引用NLOG,然后使用Logmanager创建一个记录器。

这意味着您没有programup.cs等中的所有电线。

最新更新