When writing Azure Functions in C#, I find it very useful that I am able to output some selected messages to a logger, and that those messages show up in the "Invocation Traces" in the Azure Portal:
This has worked just fine, from the Azure Function v1 up to those written against .NET 6.0 Isolated.
But now, I've created a few more new functions, using .NET 8.0 Isolated, and now it seems, outputting those log/trace messages no longer works....
What has changed so fundamentally that my logging no longer shows up in the Azure Portal?
Right now, I'm injecting the ILoggerFactory
into my Azure Function class and creating an ILogger<T>
from it (where T
is the type of my function class).
public class MyAzureFunction
{
private readonly IConfiguration _configuration;
private readonly ILoggerFactory _factory;
private readonly ILogger<MyAzureFunction> _logger;
public MyAzureFunction(IConfiguration configuration, ILoggerFactory factory)
{
_configuration = configuration;
_factory = factory;
_logger = factory.CreateLogger<MyAzureFunction>();
}
// Actual Azure Function code follows here
}
I also tried to use the FunctionContext
as an injectable parameter to my Azure Function method that gets triggered (by a HTTP call, or a timer) and create the ILogger<T>
from that:
[Function("MyAzureFunction")]
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req,
FunctionContext executionContext)
{
ILogger _logger = executionContext.GetLogger(nameof(MyAzureFunction));
_logger.LogInformation("My Azure Function was HTTP triggered");
// rest of the function code
}
Neither of these approaches seems to work - none of my own custom messages, output by _logger.LogInformation
, actually show up in the Azure Portal "Invocation Traces" display.
I've also played around with various settings in the _host.json
- but to no avail - and not knowing exactly where the problem lies, and how to fix it, this all seems like a bit of a messy "trial & error" without really understanding what the issue is, and how to solve it.
Can anyone enlighten me? What am I missing? What has changed with .NET 8.0 support in Azure Functions? How can I get my own custom messages to show up in the Azure Invocation Traces again?
You should call AddApplicationInsightsTelemetryWorkerService()
and ConfigureFunctionsApplicationInsights()
during service configuration in your Program.cs
file:
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(services => {
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
})
.Build();
host.Run();
Also note that by default, the Application Insights SDK adds a logging filter that instructs the logger to capture only warnings and more severe logs. If you want to disable this behavior, remove the filter rule as part of service configuration:
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(services => {
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
})
.ConfigureLogging(logging =>
{
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);
}
});
})
.Build();
host.Run();