DEV Community

Cover image for Fixing console logs for Azure Functions running in a Docker container
Anton Sizikov
Anton Sizikov

Posted on

Fixing console logs for Azure Functions running in a Docker container

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.

Azure Functions app in Rider

With the default Run/Debug configuration

Run configuration

We can compile and run our function

Azure Functions Log

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...

Just the main app is logging

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.

Just the main app is logging

The missing link here is the hosts.json logging configuration.

    "logLevel": {
        "default": "Information"
    }
Enter fullscreen mode Exit fullscreen mode

And voilà, we've got logs in our container:

Logs in a docker container

I hope that would save you some time. Happy logging!

This is a cross post from my personal blog.

Top comments (1)

Collapse
 
cogax profile image
Andreas Gyr

Thanks a lot! Another silly azure functions settings..