datalust / serilog-sinks-seq

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

Memory leak through use of HttpClient in SeqSink #84

Closed hampusborgos closed 7 years ago

hampusborgos commented 7 years ago

SeqSink leaks memory through creating new instances of HttpClient. HttpClient is intended to be instantiated once, and then reused for all future requests. Creating new HttpClients will leak memory and network connections over time.

You can read more details on this subject in this article: https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/

You can also find more details in the Windows azure performance manual here: https://docs.microsoft.com/en-gb/azure/architecture/antipatterns/improper-instantiation/

The fix is updating the variable here: https://github.com/serilog/serilog-sinks-seq/blob/dev/src/Serilog.Sinks.Seq/Sinks/Seq/SeqSink.cs#L65

to be static scope, and making the necessary changes to the use of the object so that it works through reuse (ie. set BaseAddress on the request, and not on the HttpClient). There is also no need to call Dispose on the object in the destructor as it has no effect (HttpClient cannot be disposed).

See this image for example of how hundreds of HttpClient objects are created (and leaked) after logging through Seq:

screenshot
merbla commented 7 years ago

Hi @hjnilsson,

Thanks for dropping by. Just wondering how you are instantiating Serilog and the Seq Sink?

nblumhardt commented 7 years ago

@hjnilsson as @merbla is suggesting, it looks like you're creating multiple instances of the Seq sink. Just like HttpClient, only a single Seq sink instance (per server, at least) should be created within an app. Check out:

https://github.com/serilog/serilog/wiki/Lifecycle-of-Loggers

for some info on how Serilog expects loggers to be created and reused.

Closing as this thread has gone quiet, but please get back in touch if you need more information or still think there's a problem here. Thanks!