Skip to content

Possible memory leak in EventSource logger? #66072

@rmja

Description

@rmja

Description

I am seeing a huge number of EventSourceLogger instances when I compare memory dumps over time from my application:
image

It seems that EventSourceLoggerProvider blindly creates new EventSourceLogger instances and adds them to the linked list. This is different from for example the console logger, which have instances in a dictionary keyed by the logger name.

What is the exact contract provided by CreateLogger(name)? Is it the provider internally that should guarantee that the same logger is returned for a given name (as the Console logger does), or is it the caller?

Of cause, if it is up to the caller to cache by name, then this is a bug in Serilog.

Reproduction Steps

Create a simple console app with the following references:

<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.EventSource" Version="6.0.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="4.2.0" />

And the following Program.cs:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;

var host = Host.CreateDefaultBuilder()
    .ConfigureServices(services => services.AddLogging(builder => builder
        .ClearProviders()
        .AddEventSourceLogger()))
    .UseSerilog((context, config) =>
    {
    }, writeToProviders: true)
    .Build();
var logger = host.Services.GetRequiredService<ILogger<Program>>();

while (true)
{
    logger.LogInformation("Hello");
}

The app will continue to allocate EventSourceLogger instances indefinitely.

Expected behavior

The EventSourceProvider should return EventSourceLogger instances keyed by name such that the linked list internally in the provider does not grow indefinitely.

Actual behavior

New EventSourceLogger instances are blindly created for every call to CreateLogger(name), which are internally stored in the linked list.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Labels

area-Extensions-Loggingbugin-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions