Fixing console logs for Azure Functions running in a Docker container
Tags: azure, azurefunctions, docker, logging
Published: 2020-07-11 15:54:06

---
title: Fixing console logs for Azure Functions running in a Docker container
published: true
tags: azure, azurefunctions,  docker, logging
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/sfcsw2r7rh95mr1t3cig.jpg
---

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](https://asizikov.github.io/images/2020-07-func-logs/initial-state.png)

With the default Run/Debug configuration

![Run configuration](https://asizikov.github.io/images/2020-07-func-logs/default-config.png)

We can compile and run our function

![Azure Functions Log](https://asizikov.github.io/images/2020-07-func-logs/log-console.png)

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: 

{% gist https://gist.github.com/asizikov/1f09de4906a61d768c2ef85b980b1a0c %}

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:

{% gist https://gist.github.com/asizikov/3528207d6fba89f70257278c68fe6ade %}

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: 

{% gist https://gist.github.com/asizikov/4ca721ce2e96ba1663f2c63fcbc50e0a %}

You would expect it to work, right?

I wish...

![Just the main app is logging](https://asizikov.github.io/images/2020-07-func-logs/func-logs.png)

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

{% gist https://gist.github.com/asizikov/7bbd14242e0a7ea198979746aa24d129 %}

Note that I'm setting `AzureFunctionsJobHost__Logging__Console__IsEnabled` environment variable here.

![Just the main app is logging](https://asizikov.github.io/images/2020-07-func-logs/docker-log.png)

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

```json
    "logLevel": {
        "default": "Information"
    }
```
{% gist https://gist.github.com/asizikov/8ec81eac670f989ab10fc4fbcaa977d9 %}

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

![Logs in a docker container](https://asizikov.github.io/images/2020-07-func-logs/docker-fixed.png)

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

_This is a cross post [from my personal blog](https://asizikov.github.io/2020/07/10/azure-function-docker-log/)._