Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling of Serilog.Events.LogEvent in LokiBatchFormatter is not thread-safe #224

Open
1 task done
k-wojcik opened this issue Nov 14, 2023 · 4 comments
Open
1 task done
Assignees
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@k-wojcik
Copy link

Which version of Serilog.Sinks.Grafana.Loki are you using?

v8.2.0

Which version of .NET are you using?

net6.0

Describe the bug

Handling of Serilog.Events.LogEvent in LokiBatchFormatter is not thread-safe.
Method AddLevelAsPropertySafely modifies Serilog.Events.LogEvent.Properties (Dictionary<string, LogEventPropertyValue>) and causes problems with simultaneous aceess to it from other threads (sinks).

https://github.com/serilog-contrib/serilog-sinks-grafana-loki/blob/master/src/Serilog.Sinks.Grafana.Loki/LokiBatchFormatter.cs#L157-L158

image

serilog/serilog#1154 (comment)
Serilog's LogEvent is not thread-safe for readers concurrent with writers; concurrent reads are fine, but only in the absence of writes

To Reproduce

Configure at least one sink with async processing alongside Serilog.Sinks.Grafana.Loki, for example I use sinks:

  • Serilog.Sinks.Async + Serilog.Sinks.File,
  • Serilog.Sinks.Seq

Expected behavior

The original value of Serilog.Events.LogEvent.Properties should never be modified in sink.

Log/SelfLog output or exception with stacktrace

System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
   at System.Collections.Generic.Dictionary`2.Enumerator.MoveNext()
   at Serilog.Formatting.Compact.CompactJsonFormatter.FormatEvent(LogEvent logEvent, TextWriter output, JsonValueFormatter valueFormatter)
   at Serilog.Sinks.Seq.ConstrainedBufferedFormatter.Format(LogEvent logEvent, TextWriter output, Boolean writePlaceholders)

Application or code sample, which could be used to reproduce a bug

No response

Additional context

No response

I have read the documentation

@k-wojcik k-wojcik added the bug Something isn't working label Nov 14, 2023
@mishamyte mishamyte added the good first issue Good for newcomers label Nov 14, 2023
@ramonesz297
Copy link

Faced the same issue, but decided to write own version, as this implementation has problems with performance (especially memory allocations).

One of optimization was to avoid any modifications of original LogEvent, that fixed the issue.

I do not think that my version can do all this package can, as my implementation target net6, net8 only, and (may be ) do not handle all cases, but for my purposes, where allocation is critical it is ok.

Anyway, maybe some ideas can be useful and applicable to this package as well.

@mishamyte
Copy link
Member

Thanks for sharing, @ramonesz297.

Yeah, I agree about the perf-related things, they are noted.
I'm also not happy with some features, that brought complexity and perf downgrade, so thinking about their future in the next release.

Hope your version will be useful for people, who struggle with some current behavior and characteristics

@oliver-unifii
Copy link

I'm evaluating this nuget and this issue alone makes it unsafe for any production environment, unfortunately.

@mishamyte
Copy link
Member

@oliver-unifii sorry to hear that and hope you will find the alternative solution, which will fit your requirements

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Development

No branches or pull requests

4 participants