Azure Application Insights 日志严重性级别
Azure Application Insights log severity levels
我想找出我的代码或方法有什么问题。我正在使用 Azure Functions (v3) 并使用 ILogger 接口记录来自我的函数的事件。
这是我使用的示例代码:
[FunctionName("GetVersion")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("[TEST] this is informational");
log.LogDebug("[TEST] this is debug");
log.LogTrace("[TEST] this is trace");
return new OkResult();
}
我的 Azure Functions 启用了 Application Insights,因此日志被写入跟踪 table,我可以这样查询它们:
traces
| order by timestamp
| where operation_Name == 'GetVersion'
| project timestamp,message,severityLevel
| limit 200
但结果看起来像这样(列是时间戳、消息,最后一列是日志严重级别):
我遇到的问题是:
- Trace 和 Debug 日志的严重性均为 0,但它们应该具有不同的级别。
- 实际严重级别与 MS 文档不同。 Trace 应该是 0 和 Debug 1 https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.loglevel?view=dotnet-plat-ext-3.1
预期的行为是 App Insights 中的严重性与文档中的严重性级别相匹配。
我在这里错过了什么?
P.S。我的 host.json 看起来像这样
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingExcludedTypes": "Request",
"samplingSettings": {
"isEnabled": true
}
},
"logLevel": {
"default": "Information",
"Host.Results": "Information",
"Function": "Debug",
"Function.GetVersion": "Trace",
"Host.Aggregator": "Trace"
}
}
}
似乎 severityLevel 在使用 ILogger
时显示不正确。我尝试使用 TraceWriter
,它按预期工作。这是我的测试样本。你可以试试看。
public static async Task<IActionResult> Run(HttpRequest req, TraceWriter log)
{
log.Info("101 Azure Function Demo - Basic logging with TraceWriter");
log.Warning("Here is a warning log message");
log.Error("Here is an error log message");
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
return name != null
? (ActionResult)new OkObjectResult($"Hello11")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}
结果
参考:
应用程序洞察的严重级别在这里:https://docs.microsoft.com/en-us/dotnet/api/microsoft.applicationinsights.datacontracts.severitylevel?view=azure-dotnet
ILogger 的“LogDebug”和“LogTrace”对于应用程序洞察来说是“详细”的。因此,据我所知,Application insights 无法区分或过滤掉您记录为“LogTrace”的内容并过滤掉您记录为“LogDebug”的内容。
我想找出我的代码或方法有什么问题。我正在使用 Azure Functions (v3) 并使用 ILogger 接口记录来自我的函数的事件。 这是我使用的示例代码:
[FunctionName("GetVersion")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("[TEST] this is informational");
log.LogDebug("[TEST] this is debug");
log.LogTrace("[TEST] this is trace");
return new OkResult();
}
我的 Azure Functions 启用了 Application Insights,因此日志被写入跟踪 table,我可以这样查询它们:
traces
| order by timestamp
| where operation_Name == 'GetVersion'
| project timestamp,message,severityLevel
| limit 200
但结果看起来像这样(列是时间戳、消息,最后一列是日志严重级别):
我遇到的问题是:
- Trace 和 Debug 日志的严重性均为 0,但它们应该具有不同的级别。
- 实际严重级别与 MS 文档不同。 Trace 应该是 0 和 Debug 1 https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.loglevel?view=dotnet-plat-ext-3.1
预期的行为是 App Insights 中的严重性与文档中的严重性级别相匹配。 我在这里错过了什么?
P.S。我的 host.json 看起来像这样
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingExcludedTypes": "Request",
"samplingSettings": {
"isEnabled": true
}
},
"logLevel": {
"default": "Information",
"Host.Results": "Information",
"Function": "Debug",
"Function.GetVersion": "Trace",
"Host.Aggregator": "Trace"
}
}
}
似乎 severityLevel 在使用 ILogger
时显示不正确。我尝试使用 TraceWriter
,它按预期工作。这是我的测试样本。你可以试试看。
public static async Task<IActionResult> Run(HttpRequest req, TraceWriter log)
{
log.Info("101 Azure Function Demo - Basic logging with TraceWriter");
log.Warning("Here is a warning log message");
log.Error("Here is an error log message");
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
return name != null
? (ActionResult)new OkObjectResult($"Hello11")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}
结果
参考:
应用程序洞察的严重级别在这里:https://docs.microsoft.com/en-us/dotnet/api/microsoft.applicationinsights.datacontracts.severitylevel?view=azure-dotnet
ILogger 的“LogDebug”和“LogTrace”对于应用程序洞察来说是“详细”的。因此,据我所知,Application insights 无法区分或过滤掉您记录为“LogTrace”的内容并过滤掉您记录为“LogDebug”的内容。