Reputation: 24067
I really don't want to introduce any delays in my high frequency trading software and at the same time I need to store thousands of lines of logs every second. 1 ms delay would be huge, I only agree to have 0.01-0.05 ms delay.
*Now*I just allocate 500 Mb in memory at start-up, store logs there and when application finish I put this log on disk.
However now I realized that I want more logs and I want them during application execution. So I now want to store logs during application execution (probably once per minute or once per 10 minute). How slow StreamWriter.WriteLine
is? Would it be slower than just "adding to preallocated collection"?
Should I use StreamWriter.WriteLine
directly (is it syncrhonous or asynchronous, is AutoFlush
option affects perfomance?). I also can use BlockingCollection
to add items to log and then use dedicated thread to process this blocking collection and to store logs on disk in another thread.
Upvotes: 0
Views: 135
Reputation: 35905
Don't
Do
If you hadn't done so, check out log4net and NLog, this will be a good place to start.
Upvotes: 1
Reputation: 15076
Use log4net as Andre Calil suggests. It logs to SQL, disks and whatnot and is extremely customizable. It can seem a bit complicated at first, but it is worth the effort.
What you need is probably the RollingFileAppender
. log4net is in nuget, but you should read the documentation at the log4net site. Start by looking at the appender config.
Upvotes: 0
Reputation: 1286
Probably you could store your logs in circular buffer and spawn a new thread of execution which will just send data from that buffer in shared memory to disk.
Upvotes: 0