问题
Is there a way to setup Serilog to keep logging in the same file while maintaining a max file size?
In other words, If I specify the max file size to be 100MB, the process should remove earlier entries from the file before adding new ones.
回答1:
TL;DR no; the File
(or its RollingFile
predecessor) doesn't provide such a facility and is unlikely to do so at any point for disk backed logs.
So, the best solution available is to set a max count of 2 logs.
The root issue with achieving what you describe is that the way most filesystems work, it would necessitate rewriting the entire file to remove the stuff being trimmed - this would mean the writer would be blocking things (and causing undue work) when this takes place. Another issue that would need to be surmounted would be managing the race condition implicit in multiple writers all trying to effect the trim at the same time (and/or with slightly different parameters).
(If you look in the Serilog Github issues list, you'll see people from time to time asking the same question in a different way by asking for a log that has the most recent information at the top of the file)
UPDATE: Someone hacked it in - that issue covers this territory ni good details - my comment there is more complete and organized than this answer ;)
来源:https://stackoverflow.com/questions/54171511/serilog-rolling-logs-in-one-file-only