gpt4 book ai didi

azure - 应用程序洞察日志记录 - 只记录我所说的内容,不记录其他内容

转载 作者:行者123 更新时间:2023-12-03 00:58:23 25 4
gpt4 key购买 nike

我有一个 Azure Function V3 (.net core)。我使用的 ILogger 来自 Microsoft.Extensions.Logging。我的问题是我无法做到:

  • 该函数仅用于使用 ILogger 记录我在 C# 代码中输入的内容。信息、异常、警告……

    例如:log.LogInformation("我的日志");

  • 内置日志记录功能没有任何内容,包括错误、异常、依赖项,什么都没有

我想要执行此操作的精确 host.json 日志记录值。

仅包含我在 C# 代码中放入的内容,仅此而已。

我的代码示例:

[FunctionName("GetNetworkHouseDetail")]
public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = "network/houseMonitoringDevices")] HttpRequest req, ILogger log, ClaimsPrincipal claims)
{
try
{
var start = DateTime.UtcNow;

// some actions

var end = DateTime.UtcNow;
var duration = (end - start).TotalSeconds;
log.LogInformation($"API - GetNetworkHouseDetail: took {duration} second to finish");
return new OkObjectResult(JsonConvert.SerializeObject(result));
}
catch (Exception ex)
{
log.LogError($"{ex.GetExceptionMessages()}", ex);
}
}

最佳答案

那么,您可以使用日志级别来实现这一点。来自 the docs :

You can use Application Insights without any custom configuration. The default configuration can result in high volumes of data. If you're using a Visual Studio Azure subscription, you might hit your data cap for Application Insights. Later in this article, you learn how to configure and customize the data that your functions send to Application Insights. For a function app, logging is configured in the host.json file.

通过使用 LogLevel None,您可以禁用给定类别和提供程序的所有日志记录。

例如,你可以这样做

{
"version": "2.0",
"logging": {
"LogLevel": {
"Default": "None",
"Host.Results": "Information",
"Host.Aggregator": "Information",
"Function.MyFunction.User": "Information"
},
}
}

它将禁用除您的功能之外的所有日志记录。您编写的函数的日志类别是“Function. .User.”。 (将 替换为代码中 FunctionName 属性中函数的实际名称)

不幸的是,您必须指定所有函数,不能包含通配符,例如指定 "Function.*.User": "Information"。您可以执行"Function": "Information"将所有函数的日志级别设置为信息,但随后也会出现一些额外的日志记录。

还将从日志记录中排除依赖项和请求跟踪。请参阅this .

如果您在任何时候想要包含请求和依赖项,则需要将以“功能”开头的所有类别的日志级别设置为信息

您可以禁用主机类别的日志记录,但它们是 special categories您现在可能想要这样做。

关于azure - 应用程序洞察日志记录 - 只记录我所说的内容,不记录其他内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64151055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com