azure-functions-dotnet-worker: Can't see logs from `ILogger.LogDebug` when running Functions locally
I use a dependency injected ILogger<T>, as such:
public class MyFunction {
private readonly ILogger<MyFunction> _logger;
public MyFunction(ILogger<MyFunction> logger) {
_logger = logger;
}
[Function(nameof(MyFunction))]
public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = "api")] HttpRequestData req)
{
_logger.LogDebug("debug");
_logger.LogInformation("information");
}
}
Running my Functions locally (func start --csharp), I am unable to see any logs with log levels below Information. In the example above, I can see “information”, but not “debug”.
I have tried many different permutations of my host.json file, including:
{
"version": "2.0",
"logging": {
"fileLoggingMode": "always",
"logLevel": {
"default": "Debug"
}
},
...
}
but it has no effect. The only way I’ve been able to see these logs is by changing the log level to something higher than Debug, which is a workaround but I have to remember to reduce the log level before deploying my code, which is annoying.
I’ve also tried running func start --verbose but it didn’t change anything.
Environment
FUNCTIONS_WORKER_RUNTIME: dotnet-isolated
func --version = 4.0.3971
.csproj file:
<TargetFramework>net6.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.10.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.7.0" />
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 6
- Comments: 15 (2 by maintainers)
The largely undocument
MinLevelofILoggingBuilderseems to be the magic sauce needed to see LogDebug and LogTrace (which you have also asked to appear in the configuration. MinLevel seems to act as a filter which doesn’t permit logs less than that level to pass, even if your config has asked for said logs to be logged.I couldn’t figure out where (but I suspect
ConfigureFunctionsWorkerDefaults) MinLevel is being set toInformationby default, which means all attempts to LogDebug and LogTrace are doomed to not do anything.And assuming you are using Function v4 and also want to use AppInsights. Many of the problems with App Insights running with isolated functions can be solved be using App Insights in the worker directly. But… that means the logging and App Insights configuration from host.json doesn’t apply. You can, however, pass in the logging configuration from App Configuration (as you would for any other .NET app).
I suspect that for .Net functions the main tricky point here is that, opposed to most modern .Net application templates, the host builder code for functions doesn’t automatically load the “Logging” section from application configuration system (appsettings.json in particular). In contrast, the WebApplicationBuilder modern ASP.NET apps start with, loads logging configuration internally using
logging.AddConfiguration(...).So basically all you need is:
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));inConfigureLogging, orlogging.SetMinimumLevel(...)call if you don’t use .net configuration, as shown above by @bdarlt. Theappsettings.jsonapproach also allows configuring AI logging.