Reputation: 61636
I am using Serilog.Sinks.RollingFile, which was recently deprecated in favor of Serilog.Sinks.File and I'll upgrade soon hopefully.
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. Example and the appsettings.json are below.
So 2 questions.
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?
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
}
}
]
}
Upvotes: 2
Views: 2156
Reputation: 31842
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.
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.
Upvotes: 2