-
Notifications
You must be signed in to change notification settings - Fork 208
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
'WriteToProviders:true' causes memory leak with EventSourceLoggerProvider
#249
Comments
Thanks for the heads-up! This looks like it's due to I'm not sure why this design is used. We should be able to get around it by caching loggers, I guess. |
Hello @nblumhardt, I have been struggling with this as well for quite a while. It seems that the EventSourceLoggerProvider is the only one (at least that I could find) that has this sort of behavior. All other ones either create brand new loggers or have internal caches to recycle previous instances. Moreover, it seems that the default LoggerFactory implementation does provide a general cache for ILoggers: https://github.com/dotnet/runtime/blob/main/src/libraries/Microsoft.Extensions.Logging/src/LoggerFactory.cs#L128 Is Serillog calling the other registered ILoggerProviders directly for this behavior to be observed? |
@nblumhardt let me know in case you need any help or further actions from my side. |
Thanks @spolaore and @raphaabreu 👍 I think we'll want to take a similar approach to MEL, here. Yes, Serilog calls the other providers directly (it completely replaces Help would be welcome if anyone can take a closer look; the relevant code is: |
I'm also experiencing memory leak but I'm not using
and
|
@ivanmonteiro not sure what's going on in your case, but if you're load testing with a lot of concurrency, it could just be that If not, to avoid derailing this thread, can you please move your comment to a new issue with some additional info about how you're exercising the code? Thanks! 👍 |
We're seeing this in 2024 -- any advice what's the best mitigation? |
nblumhardt's advice to use Serilog.Sinks.Async helped a lot. |
@abergs reducing the log level also helped |
EventSourceLoggerProvider
Description
UseSerilog(writeToProviders: true)
is causingMicrosoft.Extensions.Logging.EventSource.EventSourceLogger
instances to leak in memory. Honestly I'm not sure if this is something that could be prevented on serilog's side or should be addressed on dotnet core runtime or, maybe, just better documented on serilog's aspnetcore. As of now, our workaround has been removingEventSourceLoggerProvider
from service collection as we don't need to write into it.Reproduction
A reproduciple sample can be found here: https://github.com/spolaore/serilog-leak
Steps to reproduce:
dotnet new webapi
(with dotnet core 3.1 or net 5)bombardier --http1 -k -c 1 -d 100s https://localhost:443/weatherForecast
Expected behavior
Expected not to observe
EventSourceLogger
objects leaking in heapRelevant package, tooling and runtime versions
Serilog 4.1.0
net5.0 framework
mcr.microsoft.com/dotnet/sdk:5.0
Additional context
heapdumps-writeToProviders-enabled.zip
heapdump-writeToProviders-disabled.zip
After load testing the reproduciple sample, this is the heap dump generated with
dotnet-gcdump
tool:The text was updated successfully, but these errors were encountered: