0

I am developing a windows service in C# which will likely write several times per second to a log. I am currently using System.IO.File.AppendAllText(time + message) for each operation and I am concerned so many writes may wear out the disk quickly.

Could this be a problem? Should I use a stream and flush every few seconds? Does the windows disk buffer protect against this?

  • More things to think about: concurrency? Abnormal termination? Delay for caller? Maximum log size? Honestly...if it's not a mere exercise you'd better pick a logging framework like NLog – Adriano Repetti Aug 29 '16 at 11:06
  • There's a _lot_ of information about this on the web in general, and this site in specific. Before people will go and repeat what's already on there, please read [ask] and share your research. – CodeCaster Aug 29 '16 at 11:34
  • Thanks Adriano, I had addressed some of these issues manually and hadn't even considered others. I had superficially looked at log4net and discarded the idea of using it as overkill, but it seems I am going to need it. Code caster, I focused my search on disk damage because it was my concern, found a question discarded as non-related to programming and little else. Thanks for bringing up the "How to ensure all data..." question. – A Fernando Aug 29 '16 at 11:56

1 Answers1

2

Why you want to reinvent the wheel? Better use some ready framework eg. Log4Net, NLog, SeriLog.

"Should I use a stream and flush every few seconds?"

What if your app crash with no flush stream? Log go away and you don't know what happens.

BWA
  • 5,672
  • 7
  • 34
  • 45