Search code examples
docker.net-coredocker-swarmunhandled-exception

Persist log across Docker container restarts


So I have a microservice running as a Docker container under .NET Core and logging to Application Insights on Azure as it writes large amounts of data fetched from EventHub to a SQL Server.

Every once in a while I get an unhandled SqlException that appears to be thrown on a background thread, meaning that I can't catch it and handle it, nor can I fix this bug.

The workaround has been to set the restart policy to always and the service restarts. This works well, but now I can't track this exception in Application Insights.

I suppose the unhandled exception is written by the CLR to stderr so it appears in the Docker logs with some grepping, but is there a way to check for this on start up and subsequently log it to Application Insights so I can discover it without logging onto the Swarm cluster and grep for restart info?


Solution

  • To persist logs,

    Approach 1

    Mount docker log directory into host machine.

    Example:

    docker run -name Container_1 -v /host_dir/logs:/var/log/app docker_image:version

    Docker container will write logs in /var/log/app directory.Now logs will be persisted in /host_dir/logs directory of host across docker restart,too.

    Approach 2

    Configure logging driver like syslog or fluentd in docker. You can look at https://docs.docker.com/engine/admin/logging/overview/ for configuring it.