【发布时间】:2021-06-01 06:19:51
【问题描述】:
我正在尝试将 Serilog 与 Application Insights sink 一起用于记录目的。我可以在 Azure 门户 (Application Insights) 的搜索栏中看到日志,但如果我们在失败或性能选项卡中查看事件的时间线,则看不到相同的日志。谢谢
下面是在 FunctionStartup 中注册 Logger 的代码,然后注入到 Function 中进行日志记录:
var logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.Enrich.WithProperty("ApplicationName", "testApp")
.Enrich.WithProperty("Environment", "Dev")
.WriteTo.ApplicationInsights(GetTelemetryClient("Instrumentationkey"), TelemetryConverter.Traces)
.CreateLogger();
builder.Services.AddSingleton<ILogger>(logger);
Telementory 客户端正在从辅助方法中获取:
public static TelemetryClient GetTelemetryClient(string key)
{
var teleConfig = new TelemetryConfiguration { InstrumentationKey = key };
var teleClient = new TelemetryClient(teleConfig);
return teleClient;
}
host.json
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingExcludedTypes": "Request",
"samplingSettings": {
"isEnabled": true
}
}
}
}
【问题讨论】:
-
WriteTo.ApplicationInsights(..., elemetryConverter.Traces),这意味着日志都被写入trace,所以你肯定看不到失败。跨度>
-
是的,日志正在进入跟踪但在故障时间线中不可见
-
失败时间线是什么?应用洞察中的故障刀片?
-
是的故障刀片,它还显示日志的时间线/遥测,其中包含这些自定义跟踪和其他异常。
-
根据我的测试,在我设置 WriteTo.ApplicationInsights` 并且我的应用程序遇到异常后,应用程序洞察力可以以这种格式捕获它。给它你的意思的时间表? i.stack.imgur.com/7EQ5n.png
标签: .net-core azure-functions azure-application-insights serilog