6

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?

Cecil Dishwasher
  • 364
  • 2
  • 15
  • 2
    You can mount host directory into docker and use that directory(host) for mounting logs. – Girdhar Sojitra Sep 20 '17 at 12:39
  • @GirdharSojitra By that do you mean the Docker log, or logs in general? This is in the standard out/error stream. If you mean this as an answer, please post it as an answer. – Cecil Dishwasher Sep 20 '17 at 12:44

1 Answers1

0

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.

Girdhar Sojitra
  • 648
  • 4
  • 14
  • 1
    Unfortunately, my service does not write logs directly (except from AppInsights), but to stdout and stderr. This is not captured in the log directory that you suggest, so there is no point in mounting this directory to the Docker host. I need to capture the Docker logs, which with the default driver json-file does not seem to log to file like the documentation says. – Cecil Dishwasher Sep 26 '17 at 14:08
  • You need to use logging driver for your application. You can look at https://docs.docker.com/engine/admin/logging/overview/. – Girdhar Sojitra Sep 27 '17 at 07:31