应用程序洞察记录——只记录我所说的,不记录其他
Application insight logging - only log what I say and nothing else
我有一个 Azure Function V3 (.net core)。我正在使用来自 Microsoft.Extensions.Logging 的 ILogger。我的问题是我做不到:
这个函数只是为了记录我使用 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。
如果您在任何时候想要包含请求和依赖项,您需要将所有以“功能”开头的类别的日志级别设置为 Information
。
您可能会禁用主机类别的日志记录,但它们是 special categories,您现在可能想要这样做。
我有一个 Azure Function V3 (.net core)。我正在使用来自 Microsoft.Extensions.Logging 的 ILogger。我的问题是我做不到:
这个函数只是为了记录我使用 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.
遗憾的是,您必须指定所有函数,不能像指定 "Function.*.User": "Information"
那样包含通配符。您可以"Function": "Information"
将所有功能的日志级别设置为信息,但随后也会出现一些额外的日志记录。
这 也将 从日志记录中排除依赖项和请求跟踪。参见 this。
如果您在任何时候想要包含请求和依赖项,您需要将所有以“功能”开头的类别的日志级别设置为 Information
。
您可能会禁用主机类别的日志记录,但它们是 special categories,您现在可能想要这样做。