应用程序洞察日志记录 - 只记录我所说的,不记录其他内容 [英] Application insight logging - only log what I say and nothing else

查看:22
本文介绍了应用程序洞察日志记录 - 只记录我所说的,不记录其他内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Azure Function V3(.net 核心).我正在使用来自 Microsoft.Extensions.Logging 的 ILogger.我的问题是我做不到:

I have an Azure Function V3 (.net core). I'm using ILogger comes from Microsoft.Extensions.Logging. My problem is I can't make it to do:

  • 该函数只是为了记录我使用 ILogger 放入我的 C# 代码中的内容.信息、异常、警告、...

  • The function just to log what I put in my C# code using ILogger. Either information, exception, warning, ...

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

ex: log.LogInformation("my log");

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

Nothing from the built-in logging features including errors, exception, dependency, nothing

我想要准确的 host.json 日志记录值.

I want the exact host.json logging value that does that.

只有我在 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);
  }
}

推荐答案

好吧,您可以使用日志级别.来自 文档:

Well, you can use Log Level for that. From the docs:

您无需任何自定义配置即可使用 Application Insights.默认配置可能会产生大量数据.如果你使用的是 Visual Studio Azure 订阅,则可能会达到 Application Insights 的数据上限.在本文后面,您将了解如何配置和自定义您的函数发送到 Application Insights 的数据.对于函数应用,日志记录在 host.json 文件中配置.

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,您可以禁用给定类别和提供程序的所有日志记录.

By using LogLevel None you can disable all logging for given categories and providers.

例如,你可以这样做

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

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

It will disable all logging except for your function. The log category for a function written by you is "Function.<YOUR_FUNCTION_NAME>.User.". (Replace <YOUR_FUNCTION_NAME> with the actual name of your function, from the FunctionName attribute in your code)

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

Unfortunately you have to specify all functions, you cannot include wilcards like specifying "Function.*.User": "Information". You can do "Function": "Information" to set the log level for all functions to Information but then some extra logging will appear as well.

从日志中排除依赖和请求跟踪.请参阅.

This will exclude dependency and request tracking from the logging as well. See this.

如果您在任何时候想要包含请求和依赖项,您需要为所有以Function"开头的类别设置日志级别;到信息.

If you at any point want to include requests and dependencies you need to set the log level for all categories starting with "Function" to Information.

您可能会禁用主机类别的日志记录,但它们是 特殊类别,您现在可能想要这样做.

You might disable logging from the Host categories but they are special categories and you might now want to do that.

这篇关于应用程序洞察日志记录 - 只记录我所说的,不记录其他内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆