1

I am trying to implement logger without serilog and facing an issue with static class logging with Microsoft Ilogger. I had a similar issue for .net core but I was able to solve it with ASP.NET Core Web API Logging from a Static Class
solution I tried to implement similar in azure function but the message is logging in local console but not in azure application insights

The goal is to log in Static class with Ilogger and LoggerFactory

[assembly: FunctionsStartup(typeof(MicrosoftLoggin.Startup))] namespace MicrosoftLoggin { public class Startup : FunctionsStartup { public override void Configure(IFunctionsHostBuilder builder) { builder.Services.AddLogging(lb => { lb.AddFilter("MicrosoftLoggin", LogLevel.Information); }); builder.Services.AddScoped<ICustomer, Customer>(); FunctionsHostBuilderContext context = builder.GetContext(); ApplicationLogging.LoggerFactory = LoggerFactory.Create(builder => { builder .AddFilter("MicrosoftLoggin", LogLevel.Information) .AddApplicationInsights() .AddConsole() .SetMinimumLevel(LogLevel.Information) .AddConfiguration(context.Configuration); }); } } } 

and trying to log like this

 public interface ICustomer { public void TestLogging(); } public class Customer : ICustomer { private readonly ILogger _logger = ApplicationLogging.CreateLogger(nameof(Customer)); public Customer(ILogger<Customer> logger) { Logger = logger; } private ILogger<Customer> Logger { get; } public void TestLogging() { _logger.LogWarning("With factory logger"); Logger.LogInformation("With Microsoft Logger"); } } 

attached the console log screnshort enter image description here for reference

I am able to see the "With Microsoft Logger" not the "With factory logger" message.

enter image description here

1
  • You need to edit the host.json code to log the information also! Commented Oct 10, 2022 at 14:14

1 Answer 1

0

On your Startup.cs, remove these:

ApplicationLogging.LoggerFactory = LoggerFactory.Create(builder => { builder .AddFilter("MicrosoftLoggin", LogLevel.Information) .AddApplicationInsights() .AddConsole() .SetMinimumLevel(LogLevel.Information) .AddConfiguration(context.Configuration); }); 

Also just add builder.Services.AddLogging(); without config for its LogLevel is already set to Information by default.

Update your Customer class into the following:

public class Customer : ICustomer { private readonly ILogger _logger; public Customer(ILogger<Customer> logger) { _logger = logger; } public void TestLogging() { _logger.LogWarning("With factory logger"); } } 

You don't need the LoggerFactory, the ILogger is injected by the DI container for you. If you're running your function app locally and you want to pass your logging to Application insights, make sure you have the APPINSIGHTS_INSTRUMENTATIONKEY and APPLICATIONINSIGHTS_CONNECTION_STRING values placed in your config file, probably inside your appsettings.json. You can find these values on the Configuration section of your Azure function that's running on Azure.

Sign up to request clarification or add additional context in comments.

4 Comments

Logging with DI is working as expected I don't have any issue with that but logging created with LoggerFactory is not logging in APPLICATIONINSIGHTS and the reason why I am trying to use LoggerFactory is to log in static classes also @raffy
@RakeshT.R have you tried my suggested approach on your static classes? Did it work?
How can we use ILogger in static class it can be used in class with Constructor Dependency Injection only right @raffy
@RakeshT.R you can try migrating your function from in-process to isolated. The isolated function supports dependency injection for ILogger<T>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.