【问题标题】:How do I configure NLog to write to Azure App Service's log stream?如何配置 NLog 以写入 Azure 应用服务的日志流?
【发布时间】:2020-04-03 17:41:56
【问题描述】:

我正在将 .NET Core 2.2 应用发布到 Azure 应用服务。我已将应用服务配置为将日志保存到 Azure 存储 blob。我正在尝试切换到使用 NLog,因为我无法在这些日志中看到范围。

我已按照NLog's documentation 上的指南进行操作;在 .csproj 中添加了 PackageReferences,按照说明更改了 Program.cs,并在 Startup.cs 中删除了对 AddLogging 的调用。当我在本地调试我的应用程序时,我可以看到 NLog 在我的调试日志中输出我期望的内容。当我发布时,日志流仍然以与以前相同的方式显示。我的 Blob 存储中的日志文件似乎也没有被 NLog 格式化。

内部 NLog 文件没有报告我的目标有任何问题。我可以使用远程调试,我可以看到 NLog 正在写入调试。

我的csproj摘录

  <ItemGroup>
    <None Include="NLog.config">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    </None>
  </ItemGroup>

我的 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="Info"
      internalLogFile="./internal-nlog.txt"
      internalLogToConsole="true"
      internalLogToConsoleError="true"
      internalLogToTrace="true"
      internalLogIncludeTimestamp="true"
      throwExceptions="true"
      throwConfigExceptions="true">

  <!-- enable asp.net core layout renderers -->
  <extensions>
    <add assembly="NLog.Web.AspNetCore" />
  </extensions>
  <time type="FastLocal" />
  <!-- the targets to write to -->
  <targets>
    <!-- 2020-04-02 20:00:12.062 +00:00 [Trace] Hangfire.Server.ServerJobCancellationWatcher: No newly aborted jobs found. -->
    <target xsi:type="Console"
            name="ConsoleTarget"
            layout="> ${longdate:universalTime=true} [${level}] ${logger}.${callsite:className=false}:${when:when=length('${ndlc}')>0:inner= [${ndlc}]} ${message} ${exception:format=tostring}"
            encoding="utf-8"
            error="false"
            detectConsoleAvailable="true" />

    <target xsi:type="File"
            name="allfile"
            fileName=".\nlog-all-${shortdate}.log"
            layout="${longdate:universalTime=true} [${level}] ${logger}.${callsite:className=false}:${when:when=length('${ndlc}')>0:inner= [${ndlc}]} ${message} ${exception:format=tostring}" />

    <!-- For Azure Log Stream -->
    <target xsi:type="Trace"
            name="azureLogStream"
            rawWrite="true"
            layout="X ${longdate:universalTime=true} [${level}] ${logger}.${callsite:className=false}:${when:when=length('${ndlc}')>0:inner= [${ndlc}]} ${message} ${exception:format=tostring}" />
  </targets>

  <!-- rules to map from logger name to target -->
  <rules>
    <logger name="Microsoft.EntityFrameworkCore.*"
            level="Information"
            final="true" />
    <logger name="Microsoft.*"
            maxlevel="Information"
            final="true" />
    <logger name="Hangfire.*"
            minlevel="Trace"
            final="true" />

    <!-- BlackHole without writeTo -->
    <logger name="*"
            minlevel="Trace"
            writeTo="azureLogStream,allfile" />

    <logger name="*"
            minlevel="Trace"
            writeTo="ConsoleTarget" />
  </rules>
</nlog>

我已将 NLog 配置为也写入文件,并且可以正常工作。

来自流日志

2020-04-03 17:31:13.241 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://**BEEP**/
2020-04-03 17:31:13.242 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 6.3456ms 404

来自NLog制作的日志文件

2020-04-03 16:02:59.9714 [Info] Namespace.Class.McWritey: [RequestId:800022a7-0000-e300-b63f-84710c7967bb RequestPath:/signalr/notifications TransportConnectionId:-qK6yuSdbcbr5PDDXJ-EBQ [Id: `ac5c3f80-456a-48b5-9122-6836f911e141`]] CloseFile File Closed by -qK6yuSdbcbr5PDDXJ-EBQ
2020-04-03 16:03:00.5218 [Info] Namespace.Class.McWritey: [RequestId:800022a7-0000-e300-b63f-84710c7967bb RequestPath:/signalr/notifications TransportConnectionId:-qK6yuSdbcbr5PDDXJ-EBQ] OnDisconnectedAsync Client Disconnected: -qK6yuSdbcbr5PDDXJ-EBQ

当我将 VS 附加到我已发布的应用程序时,来自调试日志

X 2020-04-03 17:39:19.1176 [Info] Namespace.Class.McWritey: [RequestId:8000161a-0000-f400-b63f-84710c7967bb RequestPath:/signalr/notifications TransportConnectionId:7ML3GmVzsm7mHPBl0sB-GA] OnConnectedAsync Client Connected: 7ML3GmVzsm7mHPBl0sB-GA 

【问题讨论】:

    标签: c# asp.net nlog


    【解决方案1】:

    是的,看起来像 ASP.NET Core,然后 System.Diagnostics.Trace 不会重定向到应用程序诊断日志。它仅适用于经典 ASP.NET。

    您可以activate log stream from log-files,因此它将监视存储在 HOME 文件夹中的任何以 .txt 或 .log 结尾的文件。然后你可以使用文件目标:

    <?xml version="1.0" encoding="utf-8"?>
    <nlog>
       <targets async="true">
          <target name="logfile" xsi:type="File" filename="${environment:HOME:cached=true}/logfiles/application/app-${shortdate}-${processid}.txt" />
       </targets>
       <rules>
          <logger name="*" minlevel="Info" writeTo="logfile" />
       </rules>
    </nlog>
    

    另见:https://github.com/NLog/NLog.Extensions.Logging/wiki/NLog-cloud-logging-with-Azure-function-or-AWS-lambda#writing-to-azure-diagnostics-log-stream

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-05
      • 1970-01-01
      • 2021-11-10
      • 1970-01-01
      • 2021-04-23
      • 1970-01-01
      • 1970-01-01
      • 2019-01-18
      相关资源
      最近更新 更多