Serilog Rolling logs in one file only

杀马特。学长 韩版系。学妹 提交于 2019-12-25 02:29:15

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!