1

Is it possible, using Serilog, to log to a webservice of mine and if throws an error (no internet, for instance) to log to a RollingFile. Should only log to RollingFile if WebService fails.

hpinhal
  • 512
  • 6
  • 22

1 Answers1

3

You can implement this yourself by creating a custom sink that wraps another new RollingFileSink(...) and only forwards events if the web service call fails.

To do this you'd implement ILogEventSink or, if the web service accepts batches, create a subclass of PeriodicBatchingSink.

Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101
  • I've been using this approach with a `FileSink` as a fallback. In order to accomplish this, I create a new `FileSink` and pass it into `MyCustomSink`. `MyCustomSink` forwards any events to the `FileSink` if the original delivery method fails. However, it appears that support for this approach is going away. In https://github.com/serilog/serilog-sinks-file/commit/b660b51289599f2e6c9ce83dc6fdb5e8c81f1369, the public `FileSink` constructor was marked obsolete, indicating that you must use `.WriteTo.File`. However `.WriteTo.File` doesn't support this use case. Is there an alternative I'm missing? – blachniet Jun 05 '19 at 17:52
  • 1
    Hey @blachniet - yes, sorry - I should have mentioned in that obsoletion message that you can now construct instances of any sink by using `LoggerSinkConfiguration.Wrap()`. If you need a hand and can raise a question here, send me a note and I'll help where I can. – Nicholas Blumhardt Jun 06 '19 at 05:03