1

I am looking into Serilog as an alternative to log4net (I have an issue where log4net stops logging with no clear way to recover from this). This is c#, mainly WinForms.

First, here are the app.config settings:

<add key="serilog:write-to:RollingFile.pathFormat" value="q:\SomeApp-%COMPUTERNAME%-log-{Date}.txt" />
<add key="serilog:write-to:RollingFile.outputTemplate" value="{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level}] {Message}{NewLine}{Exception}" />
<add key="serilog:write-to:File.rollOnFileSizeLimit" value="true" />
<add key="serilog:write-to:File.fileSizeLimitBytes" value="10485760" />
<add key="serilog:write-to:File.retainedFileCountLimit" value="10" />
<add key="serilog:minimum-level" value="Information" />

In the C# app, I create the logger like this:

Log.Logger = new LoggerConfiguration()
                .ReadFrom.AppSettings()
                .CreateLogger();

Question: CreateLogger gives no indication of an issue even when I specify an invalid path in "serilog:write-to:RollingFile.pathFormat" above, but of course nothing gets logged.

Logging is critical, so if it does not work I need to know about it. How can I tell?

Lars335
  • 1,730
  • 2
  • 22
  • 35
  • I am not familiar with Serilog, however, I use log4net often and in almost all cases when log4net stops logging is because of a code crash or system type failure. I am curious if you know “why” log4net stops logging? If it’s a crash, then you should fix that as any internal logger would stop logging. Is it possible to demonstrate with some code where log4net “stops logging” with no way to recover? – JohnG Feb 05 '19 at 05:04
  • @JohnG I've seen the same in the past (for example if backup software momentarily locks one of the files for reading while the log file rolling is rippling through renaming, it can end up freezing the logging permanently (yes, it seems insane - having said that, this was 14y ago, maybe they fixed it!)) (Serilog doesn't do renaming, and is not written in a way that would lead it to fail permanently) – Ruben Bartelink Feb 05 '19 at 07:18
  • @JohnG, I have a separate question re. log4net (I would rather get this working than switch to a different logger): https://stackoverflow.com/questions/54526848/log4net-stops-logging-how-to-recover – Lars335 Feb 05 '19 at 18:02
  • After looking at your original post, I am confident this may be out of my skill level. However, from what I understand, the “shared” drive is used to place the log file on. On occasion, this “shared” drive gets lost just as the logger is trying to write to it. I am guessing that this may be the case for just about “any” logger, and you may end up in the same boat trying different loggers. Obviously, this is just a comment as I really do not know this from experience, so I guess this certainly couldn’t hurt. – JohnG Feb 06 '19 at 03:30
  • I am guessing that in the connection string you may have already tried to set the `log4.net` `ReconnectOnError` property to true, as its default value is false. In addition to setting a `ConnectTimeout` and `ConnectRetryCount` value. I have not tried this and really have no idea if this may solve the issue. I also assume you have already perused this SO question and answer… [log4net to SQLServer : what happens if database is unavailable?](https://stackoverflow.com/questions/3778785/log4net-to-sqlserver-what-happens-if-database-is-unavailable) … this appears to be related to your issue. – JohnG Feb 06 '19 at 03:30

1 Answers1

1

Serilog, like most typical loggers will not impede app progress by default. There is a .Audit.To config mechanism that can make it do that, however its very unlikely that it's going to be the answer for you.

In general, wiring Serilog's SelfLog to a failsafe sink is how highlightint failures in the logging system is best managed.

Aside: It also seems you're mixing RollingFile and File config - File has superseded the former so its' recommended to use solely that for new systems.

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
  • for some applications, logging is critical enough that the application should not continue processing until the logging is working again. In my test-case above, I intentionally specified a path to a non-existent drive and got no indication that anything was wrong (though it does throw an exception if the path format is invalid, like "ABC:\temp.txt"). If you know of a logger that works better for my needs, please let me know... – Lars335 Feb 05 '19 at 18:00
  • @lars335 I understand and appreciate your concern; however, that's a separate debate; bottom line is that most loggers _do_ by convention have the semantics I describe. _And, as stated, there is a mechanism to give it your semantics - the `AuditTo` mechanism_. I strongly suggest you follow the links and read - Serilog has massively wide use and is very resilient, but it is just a logger. There may be a debate to be had about providing a way aside from SelfLog to determine whether a config is entirely broken - perhaps [the serilog gitter](https://gitter.im/serilog/serilog) is the place for that – Ruben Bartelink Feb 05 '19 at 19:16