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 for reference
I am able to see the "With Microsoft Logger" not the "With factory logger" message.