简体   繁体   English

从多台计算机使用Serilog滚动文件接收器的正确方法

[英]Proper way to do rolling file sink with Serilog from multiple machines

I am using Serilog.Sinks.RollingFile , which was recently deprecated in favor of Serilog.Sinks.File and I'll upgrade soon hopefully. 我正在使用Serilog.Sinks.RollingFile ,而最近不赞成使用Serilog.Sinks.File ,我希望很快会升级。

For now, my question is how to properly log to a shared log file from multiple machines (either with the new or old package). 现在,我的问题是如何从多台计算机(使用新软件包或旧软件包)正确登录到共享日志文件。 When 10 different servers are logging to this single file, sometimes fragments of the lines end up on different lines. 当10个不同的服务器记录到该文件时,有时这些行的片段最终会出现在不同的行上。 Example and the appsettings.json are below. 示例和appsettings.json在下面。

So 2 questions. 所以有两个问题。

  1. How do I resolve the problem of fragments ending up on different lines. 如何解决以不同行结尾的片段的问题。 Or is this problem inherent with writing to file from multiple machines at once? 还是一次从多台机器写入文件时固有的问题?

  2. If this something the newer library 如果这是新的库

在此处输入图片说明

"Serilog": {
    "WriteTo": [
      {
        "Name": "RollingFile",
        "Args": {
          "pathFormat": "\\\\server\\share\\log-{Date}.log",
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} | {Level:u3} | {MachineName} | {SourceContext} | {RequestId} | {RequestPath} | {ThreadId} | {Message}{NewLine}{Exception}",
          "shared": true
        }
      }
    ]
  }

The generally-accepted way to do this is for each app instance to take an exclusive lock on the file prior to writing, and release it when done. 通常接受的方法是让每个应用程序实例在写入之前对文件进行排他锁,并在完成后释放它。

Unfortunately, this requires opening and closing the file a lot, which is VERY slow, and serializes work, which has a cost as the number of machines grows. 不幸的是,这需要大量打开和关闭文件,这非常慢,并且需要序列化工作,随着机器数量的增加,这会产生成本。 Serilog doesn't implement this currently, since its one of those features that appears to work in testing, but performs very poorly in production. Serilog目前未实现此功能,因为它的功能之一似乎可以在测试中使用,但在生产中却表现不佳。

Having each app instance write to its own file is a better scheme, but using a network-based log collector/service will be less hassle in the long run. 将每个应用程序实例写入其自己的文件是一个更好的方案,但是从长远来看,使用基于网络的日志收集器/服务将不会有太多麻烦。

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

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