如何使用.net3.1中的c#控制台应用程序中的docker创建serilog logger文件

发布于 2025-01-31 07:15:02 字数 419 浏览 6 评论 0原文

我想使用Docker Image创建Serilog Logger文件。我能够在控制台应用程序中创建一个文件,但无法使用Docker创建文件。 这是我的示例代码,

Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .WriteTo.Console()
                .WriteTo.File(fileName, rollingInterval: RollingInterval.Day)

                .WriteTo.File(@"C:\log.txt", rollingInterval: RollingInterval.Day)
                .CreateLogger();

I want to create serilog logger file using docker image. I am able to create a file in console app but not able to create using docker.
Here is my sample code,

Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .WriteTo.Console()
                .WriteTo.File(fileName, rollingInterval: RollingInterval.Day)

                .WriteTo.File(@"C:\log.txt", rollingInterval: RollingInterval.Day)
                .CreateLogger();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰雪梦之恋 2025-02-07 07:15:02

简而言之,Docker的想法是将应用程序与正在运行的主机隔离。您的应用程序正在尝试访问位于主机而不是容器上的“ C:\ log.txt”文件。您可以通过指定诸如'/app/log.txt'之类的路径(注意linux型)来写入容器内的文件。
如果您确实需要主机端日志文件,则需要更复杂的方法 - 因此答案可能会有所帮助:

在Docker容器之外露出日志文件

In short, the docker idea is to isolate the application from the host it is running on. Your app is trying to access the 'c:\log.txt' file which resides on host, not the container. You can write to the file inside the container by specifying the path like '/app/log.txt' (note the linux-style).
If you really need the host-side log file, the more complicated approaches are required - this SO answer may help:

Expose log file outside docker container

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文