Fix console logs for Azure Functions running in a Docker container
Published on:
Local development of C# Azure Functions on macOS is still a bit painful.
Even the simple-ish logging might cause issues. Let’s assume that we have Azure Functions Core Tools installed and we have a basic function app with one TimerTrigger
function created.
With the default Run/Debug configuration
We can compile and run our function
Our function will start and log to console as expected.
I wish all the functions were that simple, right? Unfortunately, sometimes we need more, sometimes we even have to use some shared libraries. So let’s create one:
Nothing fancy there, one class, one interface accepting the ILogger<T>
as a dependency.
Let’s update function code accordingly: will make is non-static, and use construction injection to get the IAsyncGreeter
injected:
In order to register this external dependency and use a propper .NET Core-style DI we need to install Microsoft.Azure.Functions.Extensions
NuGet package and create a Startup
class like this one:
You would expect it to work, right?
I wish…
As you can see it still just logs from the function.
Why would one logger print it’s output to console while another one just flushes our logs to void?
Because they are two different loggers with different settings. One logger is instantiated and configured in our Startup.cs
class and another one is built and configured by function host and injected into the method instead. Oh, well.
I want to run this function in docker, so let’s create a Dockerfile
.
Note that I’m setting AzureFunctionsJobHost__Logging__Console__IsEnabled
environment variable here.
The missing link here is the hosts.json
logging configuration.
"logLevel": {
"default": "Information"
}
And voilĂ , we’ve got logs in our container:
I hope that would save you some time. Happy logging!