TimBunting
TimBunting

Reputation: 542

Azure Function using Insights with Log Analytics Workspace not displaying Information level messages

I have created a simple Azure Function in .NET with a HTTP Trigger.

The program.cs looks like:

var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(services =>
{
    services.AddApplicationInsightsTelemetryWorkerService();
    services.ConfigureFunctionsApplicationInsights();
})
.Build();

host.Run();

and the hosts.json looks like:

hosts.json

My function spits out some logging when called:

        _logger.LogDebug($"Debug ");
        _logger.LogInformation($"Information");
        _logger.LogWarning($"Warning ");
        _logger.LogError($"Error ");
        _logger.LogCritical($"Critical");

I deploy this into Azure Function with Insights, and Log Analytics Workspace.

However when I look in the Azure Insights I only see the Warning, Error and Critical error message.

Changing host.json and redeploying seems to make no difference.

Any idea how I can get Information messages in Insights?

Thanks

Upvotes: 1

Views: 235

Answers (1)

RithwikBojja
RithwikBojja

Reputation: 11393

Any idea how I can get Information messages in Insights?

You can use below code to get desired result:

Function1.cs:

using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;

namespace FunctionApp93
{
    public class Function1
    {
        private readonly ILogger _logger;

        public Function1(ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger<Function1>();
        }

        [Function("Function1")]
        public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req, FunctionContext context)
        {
            var logger = context.GetLogger("Function1");
            logger.LogInformation("Hello Rithwik Bojja, This is From LogInformation");
            logger.LogWarning($"Hello Rithwik Bojja, This is From LogWarning ");
            logger.LogError($"Hello Rithwik Bojja, This is From LogError ");
            logger.LogCritical($"Hello Rithwik Bojja, This is From LogCritical");
            var response = req.CreateResponse(HttpStatusCode.OK);
            response.Headers.Add("Content-Type", "text/plain; charset=utf-8");

            response.WriteString("Welcome to Azure Functions!");

            return response;
        }
    }
}

host.json:

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    },
    "logLevel": {
      "FunctionApp93.Function1": "Information"
    }
  }
}

Program.cs:

using Microsoft.Extensions.Hosting;
using Serilog;

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureServices(services =>
    {
        
        Log.Logger = new LoggerConfiguration()
            .CreateLogger();
    })
    .Build();

host.Run();

Output:

enter image description here

In Portal:

enter image description here

Upvotes: 0

Related Questions