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.16k stars 81 forks source link

Multiple files on the fly #8

Closed cirrusone closed 4 years ago

cirrusone commented 4 years ago

Hello,

Firstly, this project looks excellent and seems to compliment your other libraries. I have a few questions

1) Is there any thought of logging MessagePack serialized byte[] straight to file with headers to speed up reading them? Eg multiple serialized objects written to same file. 2) Often I need to log to files where the filename is unknown until time of logging. Is it possible to create new filenames when logging? For example, Serilog uses sinks and NLog has variables. Eg something like this...

// Serilog
string logPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), $"SomePath");
Log.Logger = new LoggerConfiguration().WriteTo.Map("Filename", "DefaultFileName.txt", (name, wt) => wt.File($"{logPath}/{name}",
                        outputTemplate: "{Message}{NewLine}",
                        flushToDiskInterval: TimeSpan.FromSeconds(1),
                        encoding: Encoding.UTF8
                        )).CreateLogger();

        [Benchmark]
        public void SerilogWriter2()
        {
            for (int i = 0; i < N; i++)
            {
                for (int m = 0; m < M; m++)
                {
                    Log.ForContext("Filename", $"{m}.txt").Information(s);
                }
            }

            Log.CloseAndFlush();
        }

// NLog 
        [Benchmark(Baseline = true)]
        public void NLogWriter()
        {
            for (int i = 0; i < N; i++)
            {
                for (int m = 0; m < M; m++)
                {
                    loggerBf.WithProperty("VarFilename", $"{m}.txt").Info(s);
                }
            }

            // Important - https://github.com/NLog/NLog/wiki/Tutorial#5-remember-to-flush
            // Flush and close down internal threads and timers
            NLog.LogManager.Shutdown();
        }
neuecc commented 4 years ago

Thanks.

  1. It's quite difficult now because it's tightly coupled to string generation (message, JSON).

  2. No. It will be hard to implement because they are deliberately shrinking the pipeline for performance.