【发布时间】:2019-05-22 10:40:25
【问题描述】:
我有一个简单的 Azure 函数,我希望能够在流式日志窗口和 Application Insights 中监控日志输出。
到目前为止,我可以在 Application Insights 中看到 NLog 输出,但在流窗口中看不到。 Microsoft ILogger 输出出现在两者中。
这是我到目前为止所做的:
- 创建了一个基本的 Azure 测试函数
- 我从 Azure 门户启用了 Application Insights
- 为函数安装了 NLog 和 Microsoft.ApplicationInsights.NLogTarget nuget 依赖项。
- 向函数添加了代码,以编程方式创建 Application Insights nlog 目标。
- 读完这些问题...Azure Log Streaming with NLog 和How to integrate NLog to write log to Azure Streaming log 我还以编程方式添加了一个 NLog TraceTarget,因为他们建议流式日志机制仅显示 Trace 输出流(不要与调用的日志级别混淆“追踪”!)
-
最后,我修改了 host.json 以将默认日志记录级别提高到“Trace”。这是流窗口显示 Microsoft ILogger 的跟踪级别输出所必需的,我认为这可能是没有显示 Nlog 输出的原因...
{ "version": "2.0", "logging": { "logLevel": { "default": "Trace" } } }
到目前为止,结果是:
- 所有 Microsoft 记录器输出都显示在流式窗口和 Application Insights 日志中。
- Nlog 输出显示在 Application Insights 中,但不显示在流式窗口中。
这是最终的功能代码...
public static class Function1
{
[FunctionName("LogTest")]
public static void Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log)
{
// Set up NLOG targets and logger
var config = new LoggingConfiguration();
//send logging to application insights
config.LoggingRules.Add(
new LoggingRule("*", LogLevel.Trace,
new ApplicationInsightsTarget()));
//also try to log to Trace output
//'RawWrite' is used to try and force output at all log-levels
config.LoggingRules.Add(
new LoggingRule("*", LogLevel.Trace,
new TraceTarget {RawWrite = true}));
LogManager.Configuration = config;
var nlog = LogManager.GetLogger("Example");
//log using native
log.LogInformation($"log:Info"); //appears in live-stream, app-insights
log.LogError("log:Error"); //appears in live-stream, app-insights
log.LogTrace("log:Trace"); //appears in live-stream, app-insights (after modifying host.json)
//log using nlog
nlog.Info("nlog:info"); //appears in ........... app-insights
nlog.Error("nlog:error"); //appears in ........... app-insights
nlog.Trace("nlog:trace"); //appears in ........... app-insights
//say goodbye
log.LogInformation("log:ending");
}
}
提前感谢您的任何建议 - 毫无疑问,我错过了一些简单的步骤。
【问题讨论】:
-
感谢卡洛斯,但正如我在帖子中已经提到的,我已经阅读了该问答和相关的问答。这两个都是指使用配置文件,而我正在尝试使用(等效)编程接口来做到这一点。
-
我稍后会更详细地检查这些链接,但只是重申一下... NLog _正在使用与 TraceTarget 完全相同的规则正确记录在 ApplicationInsights 目标上。问题是 TraceTarget 输出没有进入 Azure 门户中的流式日志控制台。第一个链接中有一个有趣的部分,它似乎显示了如何将 NLog 连接到 ILogger 实例,所以我稍后再试。
标签: c# azure-functions nlog