使用 NLog 进行 Azure 日志流式传输

Azure Log Streaming with NLog

我正在尝试 nlog 使用 Azure webapp 日志流。

如果我不使用 nlog,日志确实会出现,而只使用 System.Diagnostics.Trace.WriteLine

但是,如果我在 nlog.config 中使用 Trace 类型,它不会显示跟踪日志 ...

<?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 xsi:type="Trace" name="trace" layout="${message}" />
    </targets>

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

我看不出我所做的与此处接受的答案有什么不同...

请注意,我削减了 nlog.config 文件以仅显示跟踪 - 但我通常也有一个 File 目标类型 - 我尝试过使用和不使用它。

我已经登录到已部署的 Azure 网站,并且 nlog 配置文件已成功上传。我正在使用 Github 部署进行部署。

我在 Azure 中将日志记录设置为仅使用文件系统日志记录,我将其设置为 verbose

有什么想法吗?

NLog-Trace-Target 仅对调试级日志事件执行 Trace.WriteLine。

也许试试这里显示的自定义 MyTraceTarget:

https://github.com/NLog/NLog/issues/1968

更新 NLog 版本。 4.5 为 NLog 跟踪目标添加了新设置 rawWrite,因此它始终独立于 LogLevel 执行 WriteLine。另见 https://github.com/NLog/NLog/wiki/Trace-target

事实证明,当 Visual Studio 启用 Application Insights(我最近添加到项目中的东西)时,它已经在我的 web.config 中插入了一个 nlog 配置部分。这意味着我的 nlog.config 文件根本没有被使用。我已通过从我的 web.config 中删除该 nlog 配置部分并将 Application Insights 目标复制到我的 nlog.config 文件中来修复它。 'Trace' 目标类型现在按预期工作并出现在 Azure 流日志中。