datalust / serilog-sinks-seq

A Serilog sink that writes events to the Seq structured log server
https://datalust.co/seq
Apache License 2.0
225 stars 50 forks source link

SEQ error -> Exception while emitting periodic batch from Serilog.Sinks.Seq.SeqSink #90

Closed samdevx1029 closed 6 years ago

samdevx1029 commented 6 years ago

I am having a strange issue using a singleton instance that wraps a Serilog logger configured with SEQ. Nothing gets written to SEQ. If I replace SEQ with a rolling file, log is written. Enabling the self log revealed there is a problem only when using SEQ.

To reproduce this error, see the unit test named Serilog_Singleton_TestError() in the attached VS2017 unit test project. UnitTestProject1.zip

In summary, follow these steps

1. Get SEQ up and running
2. Create VS2017 + .NET Framework 4.7 + Unit Test Project
3. Install packages Serilog and Serilog.Sinks.Seq
4. Create a singleton class wrapping a Serilog instance
5. Create a unit test and log something.
6. Build. Initially the build fails because System.Net.Http is a required dependency that is missing. However, VS offers to add the reference. Accept it.
7. Run the test.

I added another unit test named Serilog_TestError() that does not use singleton. This time SEQ works. I hope this has nothing to do with the way I wrote singleton. You can try your way. I think the problem is related to System.Net.Http and SEQ I saw in other blog posts.

Any help would be greatly appreciated.

nblumhardt commented 6 years ago

Hi, thanks for the report. Could you please include the SelfLog output? Thanks!

samdevx1029 commented 6 years ago

Sure, here is the output.

2017-10-04T01:57:50.4549728Z Exception while emitting periodic batch from Serilog.Sinks.Seq.SeqSink: System.Threading.ThreadAbortException: Thread was being aborted. at Serilog.Sinks.Seq.SeqSink.d14.MoveNext() at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](TStateMachine& stateMachine) at Serilog.Sinks.Seq.SeqSink.EmitBatchAsync(IEnumerable`1 events) at Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.d16.MoveNext()

nblumhardt commented 6 years ago

Hi Sam, thanks for the additional info.

The problem is that logging to Seq happens asynchronously; the unit test framework is terminating the process before the logs have been sent.

You don't see the issue in your other unit test because you're Dispose()ing the logger, there.

Serilog's own static logger has a method CloseAndFlush() to dispose the logger before program exit; you could add the same to your Singleton class and call it from the unit test to get the same result.

HTH!

nblumhardt commented 6 years ago

Closing with the expectation that the info above will be enough to unblock you, but please get in touch if you need further help. Cheers!