Possible memory leak
jessicah opened this issue · 3 comments
Initially filed at: serilog/serilog-sinks-periodicbatching#76
Description
Serilog appears to end up with unbounded memory usage, creating a large number of CancellationTokenSources and ValueTasks.
Reproduction
Program.cs:
Log.Logger = new LoggerConfiguration()
.Filter.RateLimited()
.WriteTo.Seq("http://...", payloadFormatter: new CompactJsonFormatter(), eventBodyLimitBytes: 10485760)
.CreateLogger();
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseSerilog(Log.Logger);
LoggingRateLimiter.cs:
using Serilog;
using Serilog.Configuration;
using Serilog.Events;
namespace BlazorSSR
{
public static class LoggingExtensions
{
public static LoggerConfiguration RateLimited(this LoggerFilterConfiguration filterConfiguration)
{
var limiter = new LoggingRateLimiter();
return filterConfiguration.ByExcluding(limiter.LimitExceeded);
}
}
class LoggingRateLimiter
{
readonly object _sync = new();
Dictionary<string, DateTime> _lastSuccessTimes = [];
public bool LimitExceeded(LogEvent logEvent)
{
lock (_sync)
{
if (logEvent.Properties.TryGetValue("Endpoint", out var path))
{
if (_lastSuccessTimes.TryGetValue(path.ToString(), out DateTime lastSuccess))
{
if (lastSuccess.AddSeconds(10) < DateTime.Now)
{
_lastSuccessTimes[path.ToString()] = DateTime.Now;
return false;
}
return true;
}
else
{
_lastSuccessTimes[path.ToString()] = DateTime.Now;
return false;
}
}
return false;
}
}
}
}
Expected behavior
Memory to remain fairly constant
Relevant package, tooling and runtime versions
- .net 8.0.200 / aspnetcore
- serilog.extensions.hosting 8.0.0
- serilog.formatting.compact 2.0.0
- serilog.sinks.async 1.5.0
- serilog.sinks.seq 7.0.0
Thanks for raising this; closing as duplicate of serilog/serilog-sinks-periodicbatching#76, until we figure out what project the issue is in.
I removed seq as a sink, and used async console sink instead, and the memory leak seems to have gone away.
Thanks for following up. It'd be great to get a few more details over on the other ticket regarding your testing conditions, just so we know whether there's something for us to chase down or not. Thanks again!