简体   繁体   中英

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

I an trying to log details of my function app into Application Insights.

My basic code:

    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);
        }
    }

Host.Json:

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

Here are few strange things I have noticed (not sure where I am doing wrong).

  1. Unhandled Exception is logging twice in Application Insights. 在此处输入图像描述

  2. Any specific reason to inject ILogger<ClassName> logger when function run method is having ILogger log by default.

  3. Main concern is that I am seeing a lot of unwanted logs in trace table, where I am just expecting it would have information that I log in code with log.LogXXX(message) . Is there a way that I can stop loading unwanted data Trace table, because it would increase the cost. 在此处输入图像描述

  4. I am not see those logs messages that I am logging from code (I have tried after 10 mins, as it might take some time to load into trace tables) , but I can see those logs in Live metrics.

在此处输入图像描述

Can someone suggest me on the above, It would be really helpful.

Kind regards.

Thanks @Hooman Bahreini, According to SO-Thread it says,

ILogger : is responsible to write a log message of a given Log Level . ILoggerProvider : is responsible to create an instance of ILogger (you are not supposed to use ILoggerProvider directly to create a logger). ILoggerFactory : you can register one or more ILoggerProvider s with the factory, which in turn uses all of them to create an instance of ILogger . ILoggerFactory holds a collection of ILoggerProviders .

We can able to see logging messages you need give correct format in quotations and column name too:在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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