【发布时间】:2021-01-22 08:22:17
【问题描述】:
我在 Azure 应用服务中托管了一个 Net Core Web API,我正在尝试以与在我的开发环境中格式化控制台相同的方式格式化 Azure 日志流的输出。
我为此使用 NLog,并使用 NLog ColoredConsole 将所有日志重定向到控制台:
"target": {
"type": "ColoredConsole",
"layout": "${var_layout}",
"rowHighlightingRules": [
{
"condition": "level == LogLevel.Trace",
"foregroundColor": "DarkGray"
},
{
"condition": "level == LogLevel.Debug",
"foregroundColor": "DarkGray"
},
{
"condition": "level == LogLevel.Warning",
"foregroundColor": "Yellow"
},
{
"condition": "level == LogLevel.Error",
"foregroundColor": "Red"
},
{
"condition": "level == LogLevel.Fatal",
"foregroundColor": "Yellow",
"backgroundColor": "Red"
}
]
}
在我的 Startup.cs 中,我删除了 AddConsole() 选项,并将 NLog 配置为将所有格式化的输出发送到 ColoredConsole。
这在我的本地控制台上运行良好,并且所有内容都按预期格式化。
但是,在 Azure 日志流中,我仍然可以看到默认格式(黑底白字)和布局(我使用 NLog 记录的自定义数据丢失)。
我的问题是:为什么 Azure 日志流没有显示格式化的输出?这不是显示控制台正在记录的确切内容吗?
如果没有,如何格式化 Azure 日志流输出?
【问题讨论】:
-
阅读有关 Azure 日志流的信息时,要么使用
AddAzureWebAppDiagnostics(写入文件系统和 Blob),要么写入/home/LogFiles目录中的日志文件。但两种解决方案都没有提供着色或突出显示。另见github.com/NLog/NLog.Extensions.Logging/wiki/… -
也许 ApplicationInsights 会根据 LogLevel 提供您需要的颜色。另见stackoverflow.com/a/58830106/193178
标签: .net-core azure-web-app-service asp.net-core-webapi nlog