serilog-contrib / Serilog.Sinks.MicrosoftTeams.Alternative

Serilog.Sinks.MicrosoftTeams.Alternative is a library to save logging information from Serilog to Microsoft Teams.
MIT License
32 stars 11 forks source link

BatchSizeLimit > 1 causes the logger to crash #7

Closed mahdiman closed 4 years ago

mahdiman commented 4 years ago

When setting BatchSizeLimit to a number > 1 the microsoft teams logger throws a null reference exception.

Log.Logger = new LoggerConfiguration().MinimumLevel.Information()
                              .WriteTo.MicrosoftTeams(webHookUri: myWebHookUri, title: "mytitle", batchSizeLimit: 100)                  
                              .CreateLogger();
Log.Logger.Information("test");

Self Log: 2020-07-13T08:38:01.3053211Z Exception while emitting periodic batch from Serilog.Sinks.MicrosoftTeams.MicrosoftTeamsSink: System.NullReferenceException: Object reference not set to an instance of an object. at Serilog.Sinks.MicrosoftTeams.MicrosoftTeamsSink.<>c__DisplayClass4_0.b__0(ExtendedLogEvent l) at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable1 source, Func2 predicate, Boolean& found) at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable1 source, Func2 predicate) at Serilog.Sinks.MicrosoftTeams.MicrosoftTeamsSink.EmitBatchAsync(IEnumerable`1 events) at Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.OnTick()

mahdiman commented 4 years ago

The problem turned out to be that inside the MicrosoftTeamsSink.cs:EmitBatchAsync:95 ... the query should check for null exceptions before accessing elements inside them as not all logEvents have exceptions.

I already fixed the issue, please give me contributor access to push changes.

SeppPenner commented 4 years ago

This should be fixed now: https://github.com/SeppPenner/Serilog.Sinks.MicrosoftTeams/commit/d38a5a276e80384669f4a626bbeb3952b8a1f0ed.

By the way, you can fork the project, edit your fork and provide a pull request.