繁体   English   中英

需要澄清:从 Azure Functions 将数据记录到 Application Insights

[英]Need Clarification : Logging data in to Application Insights from Azure Functions

我试图将我的函数应用的详细信息记录到 Application Insights 中。

我的基本代码:

    public class AzureAppInsightsExplore
    {
        private readonly ILogger<AzureAppInsightsExplore> logger;
        public AzureAppInsightsExplore(ILogger<AzureAppInsightsExplore> logger)
        {
            this.logger = logger;
        }
        [FunctionName("AzureAppInsightsExplore")]
        public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log) //Can we directly use log
        {
            // Unable to find this log in trace but in live metrics it is shown.
            logger.LogInformation("C# HTTP trigger function processed a request.");
            int a = 0, b = 0;
            //Unhandled exception
            int c = a / b;
            return new OkObjectResult(string.Empty);
        }
    }

主机.json:

{
    "version": "2.0",
    "logging": {
      "applicationInsights": {
        "samplingSettings": {
          "isEnabled": true,
          "excludedTypes": "Request"
        }
      }
    }
}

以下是我注意到的几件奇怪的事情(不知道我在哪里做错了)。

  1. 未处理的异常在 Application Insights 中记录了两次。 在此处输入图像描述

  2. 当函数运行方法默认具有ILogger log时,注入ILogger<ClassName> logger的任何特定原因。

  3. 主要担心的是我在跟踪表中看到了很多不需要的日志,我只是希望它会包含我使用log.LogXXX(message)登录代码的信息。 有没有办法可以停止加载不需要的数据Trace表,因为它会增加成本。 在此处输入图像描述

  4. 我没有看到我从代码中记录的那些日志消息(我在 10 分钟后尝试过,因为加载到跟踪表中可能需要一些时间),但我可以在实时指标中看到这些日志。

在此处输入图像描述

有人可以在上面建议我吗,这真的很有帮助。

亲切的问候。

谢谢@Hooman Bahreini,根据SO-Thread说,

ILogger :负责写入给定日志级别的日志消息。 ILoggerProvider :负责创建ILogger的实例(您不应该直接使用ILoggerProvider来创建记录器)。 ILoggerFactory :您可以向工厂注册一个或多个ILoggerProvider ,而工厂又使用所有这些来创建ILogger的实例。 ILoggerFactory拥有ILoggerProviders的集合。

我们也可以看到您需要在引号和列名中给出正确格式的日志消息:在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM