简体   繁体   English

持续登录Docker容器重启

[英]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. 因此,我在.NET Core下运行作为Docker容器的微服务,并在Azure上将大量数据从EventHub提取到SQL Server时记录到Application Insights。

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. 每隔一段时间我就会得到一个未处理的SqlException,它似乎被抛出在后台线程上,这意味着我无法捕获并处理它,也无法解决这个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. 这很好用,但现在我无法在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? 我想CLR将未处理的异常写入stderr,因此它在Docker日志中出现了一些grepping,但有没有办法在启动时检查它,然后将其记录到Application Insights,这样我就可以在不登录的情况下发现它Swarm集群和grep重启信息?

To persist logs, 要保留日志,

Approach 1 方法1

Mount docker log directory into host machine. 将docker日志目录挂载到主机。

Example: 例:

docker run -name Container_1 -v /host_dir/logs:/var/log/app docker_image:version 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. Docker容器会将日志写入/ var / log / app目录。现在,对于docker restart,主机的/ host_dir / logs目录中也会保留这些日志。

Approach 2 方法2

Configure logging driver like syslog or fluentd in docker. 配置日志驱动程序,如syslog或流利的docker。 You can look at https://docs.docker.com/engine/admin/logging/overview/ for configuring it. 您可以查看https://docs.docker.com/engine/admin/logging/overview/进行配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM