Search code examples
c#azureazure-application-insights

Azure Application Insight logs only Warning and higher


An Azure Function App (C# 8) logs to Azure Application Insights. While I can see all its log messages in the live window during "Test/Run", I find only part of these log messages when searching for them in "Application Insights - Logs - traces". And the messages which I find are at Warning level only (no exceptions happened, so not expected here). While entries of other items are even at Trace level (e.g. "Sending invocation id...")

Where do I have to configure what settings?


Solution

  • I have created an HTTP trigger function with a runtime stack of .NET 8.0.

    • I have configured Application Insights using the instrumentation key in the code and able to retrieve logs in AppInsights.

    function code:

    using System;
    using System.Net;
    using Microsoft.Azure.Functions.Worker;
    using Microsoft.Azure.Functions.Worker.Http;
    using Microsoft.Extensions.Logging;
    
    namespace Company.Function
    {
        public class HttpTrigger1
        {
            [Function("HttpTrigger1")]
            public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req)
            {
                try
                {
                    _logger.LogInformation("C# HTTP trigger function processed a request.");
    
                    throw new InvalidOperationException("This is a sample exception.");
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "An exception occurred.");
                    var errorResponse = req.CreateResponse(HttpStatusCode.InternalServerError);
                    errorResponse.WriteString("An error occurred. Please try again later.");
                    return errorResponse;
                }
            }
        }
    }
    
    

    host.json:

    {
      "version": "2.0",
      "logging": {
        "applicationInsights": {
          "samplingSettings": {
            "isEnabled": true,
            "excludedTypes": "Request"
          }
        },
        "logLevel": {
          "default": "Information",
          "MyNamespace.MyCustomExceptionCategory": "Error",
          "MyNamespace.MyInformationCategory": "Information",
          "MyNamespace.MyWarningCategory": "Warning"
        }
      }
    }
    

    Program.cs:

    using Microsoft.Azure.Functions.Worker;
    using Microsoft.Extensions.Hosting;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Logging;
    using Microsoft.Extensions.Logging.ApplicationInsights;
    
    var host = new HostBuilder()
        .ConfigureFunctionsWorkerDefaults()
        .ConfigureServices(services => {
            services.AddLogging(loggingBuilder =>
            {
                loggingBuilder.AddApplicationInsights("your appinsights instrumentation key");
                loggingBuilder.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Information);
            });
        })
        .Build();
    host.Run();
    

    Application successfully deployed and able to see in function app. check below:

    enter image description here

    When I click on Test/Run ran successfully and get the Exception logs and Information logs in Application insights. enter image description here

    Output:

    enter image description here

    enter image description here