JackFrost
JackFrost

Reputation: 120

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. enter image description here

  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. enter image description here

  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.

enter image description here

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

Kind regards.

Upvotes: 4

Views: 1026

Answers (1)

RithwikBojja
RithwikBojja

Reputation: 11253

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 ILoggerProviders 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: enter image description here

Upvotes: 0

Related Questions