Cysharp / ZLogger

Zero Allocation Text/Structured Logger for .NET with StringInterpolation and Source Generator, built on top of a Microsoft.Extensions.Logging.
MIT License
1.25k stars 88 forks source link

Is it possible to change the flush rate? #32

Closed carrotstien closed 3 years ago

carrotstien commented 3 years ago

My current logger writes to an output stream that i flush once ever 10 seconds to save on IO hits. It looks like ZLogger flushes immediately. It is possible to adjust this (as i suspect this might cause performance issues...or am i wrong?)

neuecc commented 3 years ago

ZLogger tries to write as quickly as possible. That is, it flushes when the message buffer is empty. I need to add the FlushRate setting.

carrotstien commented 3 years ago

As i'm using this for a vr application, i'm worried that writing so often to the disk (and i'm hoping on a side thread...but can't find where), might result in some frame skipping.

In my own logging implementation that I replaced with Zlogger, I wrote to a fileWriter with autoflush=0ff, and then had a side thread call flush() at some interval (i think once ever 10 seconds)

neuecc commented 3 years ago

reader is already in async reader-loop. https://github.com/Cysharp/ZLogger/blob/master/src/ZLogger/AsyncStreamLineMessageWriter.cs#L89 It is run on thread-pool so does not block in logger.

carrotstien commented 3 years ago

yea i meant more general system wide hiccup on IO. Even my powerful computer can hang for a moment when doing a heavy IO process.

Either way, i'll look forward to the FlushRate. No rush at all (in fact let me know where to donate :D ), but any estimate as to when you'll add that?

neuecc commented 3 years ago

I've released 1.4.0, it includes ZLoggerOptions.FlushRate.

carrotstien commented 3 years ago

This flush rate works mostly as expected - in that it writes to the files after the timespan. However, the log header defined only gets resolved at flush time. I was hoping the write would immediately to a buffer in memory, and that would get dumped to the system disk in flush time.

for example: builder.AddZLoggerFile(filename, "file-plain" , x => { x.PrefixFormatter = (writer, info) => ZString.Utf8Format(writer, "[{0} {1} {2}]", info.Timestamp.ToLocalTime().DateTime,MainThreadActionQueue.frameNumber, Misc.GetLongTime()); x.FlushRate = TenSeconds; });

            the frame number and time stamp are resolved at flush which make them a lot less useful for debugging :(
neuecc commented 3 years ago

Timestamp is created at Log method called. https://github.com/Cysharp/ZLogger/blob/aa70632b0e7f49b3c3e959c270646734864d4c28/src/ZLogger/AsyncProcessZLogger.cs#L26

frameNumber and GetLongTime are not concerned with this lib.