Should log file streams be opened/closed on each write or kept open during a desktop application's lifetime?

前端 未结 13 1211
小蘑菇
小蘑菇 2020-12-04 19:52

Should log classes open/close a log file stream on each write to the log file or should it keep the log file stream open throughout the application\'s lifetime until all log

13条回答
  •  一个人的身影
    2020-12-04 20:02

    I can think of a couple reasons you don't want to hold the file open:

    • If a log file is shared between several different apps, users, or app instances you could have locking issues.
    • If you're not clearing the stream buffer correctly you could lose the last few entries when that app crashes and you need them most.

    On the other hand, opening files can be slow, even in append mode. In the end, it comes to down to what your app is doing.

提交回复
热议问题