I recently seen this issue in a .NET Isolated Azure Function App, it was
writing custom logs at Information level to Application Insights, but the logs aren't there.
It was configured correctly with Application Insights in the Program.cs. And there was no logging filters configured.
using Microsoft.Azure.Functions.Worker.Builder;
using Microsoft.Extensions.DependencyInjection;
FunctionsApplicationBuilder builder = FunctionsApplication.CreateBuilder(args);
builder.Services
.AddApplicationInsightsTelemetryWorkerService()
.ConfigureFunctionsApplicationInsights();
To reproduce the issue locally, created a simple .NET isolated Azure Function
App with a HTTP trigger that logs something like this.
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
namespace FunctionApp1;
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.");
return new OkObjectResult("Welcome to Azure Functions!");
}
}
After spending some time, noticed this.
However, by default, the Application Insights SDK adds a logging filter that instructs the logger to capture only warnings and more severe logs
And to disable the behavior, we can do this.
builder.Logging.Services.Configure<LoggerFilterOptions>(options =>
{
LoggerFilterRule defaultRule =
options.Rules.FirstOrDefault(rule => rule.ProviderName == "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");
if (defaultRule is not null)
{
options.Rules.Remove(defaultRule);
}
});
And now when triggered the HTTP Function,
The logs now started to being written.
Happy Coding.
Regards,
Jaliya
Jaliya
No comments:
Post a Comment