1

I´ve searched Google & Co quite a while without any result, so sorry if my question has been answered somewhere else...

I´ve been using Serilog within Blazor server apps with no problems and decided to use Serilog for my Winforms apps as well. Conditions under which I'm using Serilog: Winforms, Framework 4.7.2, Windows 10. Unfortunately it seems that, when I'm taking a look into the written log-files, the last 1-5min of logging are missing and are never written - no matter how often I call Log.CloseAndFlush. This behavior is not exactly reproducable - sometimes all entries have been written, sometimes not.

This is how I initialize Serilog:

Serilog.Log.Logger = new LoggerConfiguration()
                .Enrich.FromLogContext()
                .MinimumLevel.ControlledBy(levelSwitch)
                .WriteTo.File(new Classes.SewCompactJSonFormatter(), 
                    m_file,
                    rollingInterval: RollingInterval.Day,
                    fileSizeLimitBytes: _50MB,
                    retainedFileCountLimit: 5,
                    rollOnFileSizeLimit: true,
                    shared: true,
                    flushToDiskInterval: TimeSpan.FromMilliseconds(1000))
                .CreateLogger();

I call the CloseAndFlush on certain user actions. But even directly after that it is not guaranteed that all logs are written.

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
AnjinSan
  • 11
  • 2
  • CloseAndFlush should not be called randomly - that can even lead to problems - it should only be called once prior to app shutdown, not during the app's lifetime. – Ruben Bartelink Apr 28 '22 at 09:28
  • Thanks Ruben, I will note that. Any other clue what might be the reason for that behavior? – AnjinSan Apr 29 '22 at 11:12
  • Can't think of any good reason - you seem to have the autoflush configured so the only thing that would block a write is another editor or viewer having the file open in such a way that it blocks the writing. However the fact that you are saying the last N minutes are missing suggests teh flush is not happening.. Main thing is you want to have that single Log.CloseAndFlush guaranteed to be called on app shutdown - in a console app, that goes in a `finally`, in a WinForms one, I can't recall (maybe in the main, maybe you need to put it in an app exiting handler). – Ruben Bartelink Apr 29 '22 at 11:41

0 Answers0