【发布时间】:2019-08-27 19:54:06
【问题描述】:
有人可以解释一下在使用 Azure Function V2 (.net Core) 时如何强制 ILogger 将某些内容实际记录到 Application Insights 中吗?
我们已配置 Application Insights,它会显示实时遥测和未处理的异常。我们要做的工作是强制应用程序洞察力存储我们通过默认 ILogger 创建的日志。
无论我们使用哪种类型的 logLevel(信息、警告、错误、严重) - 应用程序洞察中都不会存储任何内容。
我还尝试创建 500 条日志消息,希望它可以将其作为批处理推送到应用程序洞察力。
我在这里遗漏了什么明显的东西吗?有人可以建议如何使用默认的 ILogger,以便将某些内容传递给与 Azure 函数 App V2(.net 核心)关联的 Application Insights 吗?
host.json
{
"version": "2.0",
"functionTimeout": "00:10:00",
"extensions": {
"durableTask": {
"maxConcurrentActivityFunctions": 4,
"maxConcurrentOrchestratorFunctions": 1
}
},
"logging": {
"fileLoggingMode": "debugOnly",
"logLevel": {
"default": "Information"
},
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"maxTelemetryItemsPerSecond": 5
}
}
}
}
Azure Function V2、TimerTrigger、DurableOrchestrationClient:
[FunctionName("MainTriggerEntry")]
public static async Task RunMainTriggerEntry([TimerTrigger("%CRON_EXPRESSION%", RunOnStartup = false)]TimerInfo timer,
[OrchestrationClient]DurableOrchestrationClient starter, ILogger log)
{
log.LogInformation("[Information] Message!");
log.LogError("[Error]. Something occured");
log.LogCritical("[Critical] Critical issue");
for (var i = 0; i < 500; i++)
{
log.LogWarning($"[Warning] Logging to Application insights. {i}");
}
// We don't want more than one orchestrator running at the same time:
var orchestrationStatus = await starter.GetStatusAsync(OrchestratorInstanceGuid);
if (orchestrationStatus == null || !orchestrationStatus.IsBusy())
{
var instanceId = await starter.StartNewAsync(OrchestratorFunctionName, OrchestratorInstanceGuid, null);
log.LogInformation($"Triggering {OrchestratorFunctionName} function with an ID '{instanceId}'.");
}
else
{
log.LogInformation($"{OrchestratorFunctionName} function with an ID '{OrchestratorInstanceGuid}' is already running.");
}
}
我们记录的应用程序洞察中没有显示任何内容。但是失败会按应有的方式出现:
更多信息:
- Nuget 包 Microsoft.NET.SDK.Functions v1.0.26
- Azure 函数 v2 通过 APPINSIGHTS_INSTRUMENTATIONKEY 连接到应用洞察
- 应用洞察显示实时遥测和异常
- 应用洞察显示一些数据,但不是来自 ILogger
【问题讨论】:
-
彼得,就是这样。你说的对。当我转到左侧的“搜索”菜单时,我会看到我的所有日志。非常感谢。您想创建一个答案以便我接受吗?否则我可以做到。
-
看我的回答:-)
标签: azure logging .net-core azure-functions azure-application-insights