Logging to Azure Application Insights from .NET Core 2 running in a Docker Container (or Azure webapp)

Logging in .NET Core 2 is made really easy.
There is a generic logger implementation which logs to the Console and to Application Insights by default. You only have to configure the instrumentationkey like this:

{
  "ApplicationInsights": {
    "InstrumentationKey": ""   
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  }
}

So you don’t have to change anything to program.cs or startup.cs.
The default logger is injected automatically through the .NET Core runtime. So you can inject the Ilogger like this:

public class HomeController : Controller
    {
        private readonly ILogger _logger;
 
        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }
        public IActionResult Index()
        {
            _logger.LogWarning("My Warning");
            return View();
        }

Running your application with Visual Studio in IISExpress logs to Both the Console and Application Insights.

But if you run the application in a Docker container, the logs will not arrive in Application Insights. I ran into this problem and the .NET Core team pointed me to a difference when hosting the application in a Docker container. They gave me the answer for this problem:

Add the following environment variable:

ASPNETCORE_HOSTINGSTARTUPASSEMBLIES=Microsoft.AspNetCore.ApplicationInsights.HostingStartup

After adding this environment variable the logs appear in Application Insights and all is well. When running in IIS Express, the Environment variable is already set automatically.

There are many ways to add environment variables, here are a couple of ways:

  1. Add the environment variable in Windows.
  2. When you have Docker support enabled and thus have a Docker compose file:
    Open the docker-compose.override.yml file and add the variable:
version: '3'

services:
  loggingtest:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_HOSTINGSTARTUPASSEMBLIES=Microsoft.AspNetCore.ApplicationInsights.HostingStartup
    ports:
      - "80"

3. Run your Docker container from the command line:

docker run -it –rm -e “ASPNETCORE_HOSTINGSTARTUPASSEMBLIES=Microsoft.AspNetCore.ApplicationInsights.HostingStartup” -p 8080:80 mywebapi

4. In the Deployment file for Kubernetes:

    spec:
      containers:
      name: myservice
        image: mspoccrwe.azurecr.io/myservice:1  
        env:
        name: ASPNETCORE_HOSTINGSTARTUPASSEMBLIES
          value: "Microsoft.AspNetCore.ApplicationInsights.HostingStartup"

 

Update:
It appears that you not only have to configure ASPNETCORE_HOSTINGSTARTUPASSEMBLIES variable to make logging work to AppInsights from a Docker container. Also when you publish your .NET Core 2 application to a WebApp in Azure or any other host. For an Azure WebApp you have to add ASPNETCORE_HOSTINGSTARTUPASSEMBLIES as an appsetting. The value should be Microsoft.AspNetCore.ApplicationInsights.HostingStartup. After adding this appsetting, your .NET Core 2 application will succesfully log to AppInsights.

Advertenties

Geef een reactie

Vul je gegevens in of klik op een icoon om in te loggen.

WordPress.com logo

Je reageert onder je WordPress.com account. Log uit /  Bijwerken )

Google+ photo

Je reageert onder je Google+ account. Log uit /  Bijwerken )

Twitter-afbeelding

Je reageert onder je Twitter account. Log uit /  Bijwerken )

Facebook foto

Je reageert onder je Facebook account. Log uit /  Bijwerken )

w

Verbinden met %s