How to Console Log and AppInsights log Azure Isolated Function

110 views Asked by At

How does one enable console logging in addition to AppInsights? I can see the insights just fine, but when I'm running locally, I would prefer to occasionally use a Console.WriteLine(...) here and there. Currently, I can't find the setting to enable both.

What I'm running:

  • Isolated v4 Azure Function
  • Http Trigger Function
  • .NET 8
  • C# Project

What I've tried:

host.json

{
  "version": "2.0",
      "logging": {
          "console": {
              "isEnabled": true,
              "logLevel": {
                  "Function": "Debug"
              }
          },
  ...
}

note: I've tried with both "Function" and "default" under the "logLevel" setting

1

There are 1 answers

0
Pavan On

How to Console Log and AppInsights log Azure Isolated Function

I have created Isolated http trigger function with runtime stack .net 8.0.

Function code:

 public class Function1
 {
     private readonly ILogger<Function1> _logger;

     public Function1(ILogger<Function1> logger)
     {
         _logger = logger;
     }

     [Function("Function1")]
     public IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
     {
         _logger.LogInformation("C# HTTP trigger function processed a request.");
         Console.WriteLine("This message will be logged to the console.");

         return new OkObjectResult("Welcome to Azure Functions!");
     }
 }

Host.json:

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        {
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      },
      "enableLiveMetricsFilters": true
    }
  },
  "console": {
    "isEnabled": true,
    "logLevel": {
      "default": "Information"
    }
  },
  "logLevel": {
    "default": "Information",
    "Host.Results": "Information",
    "Function": "Information"
  }
}
                                                                                                                                                                                                                                                                                                                                                                                                                                     

Program.cs:

  public class Program
  {
      public static void Main()
      {
          var host = new HostBuilder()
              .ConfigureAppConfiguration(config =>
              {
                  config.AddEnvironmentVariables();
                  config.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true); 
              })
              .ConfigureFunctionsWebApplication()
              .ConfigureLogging((context, loggingBuilder) =>
              {
                  loggingBuilder.ClearProviders(); 
                  loggingBuilder.AddConsole(); 

                  var instrumentationKey = context.Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
                  if (!string.IsNullOrEmpty(instrumentationKey))
                  {
                     
                      loggingBuilder.AddApplicationInsights(instrumentationKey);
                  }
                  else
                  {
                      loggingBuilder.AddDebug(); 
                  }
              })
              .Build();

          host.Run();
      }
  }

I have added console and application insights configuration in program.cs and log levels added in host.json.

The above function executed successfully. check below:

enter image description here

I am able to get the console logs and appinsight logs in applicationinsights.check below:

Output:

enter image description here