Cysharp / ZLogger

Zero Allocation Text/Structured Logger for .NET with StringInterpolation and Source Generator, built on top of a Microsoft.Extensions.Logging.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Misuse

FoxTes opened this issue · comments

Hello @neuecc
I have the following code:

var builder = WebApplication.CreateSlimBuilder(args);
builder.Logging.ClearProviders();
builder.Logging.AddZLoggerConsole();
builder.Services.AddHostedService<StartupService>();

var app = builder.Build();
app.Run();

public class StartupService(ILogger<StartupService> logger) : IHostedService
{
    public Task StartAsync(CancellationToken cancellationToken)
    {
        while (true) logger.ZLogInformation($"yeap");
    }

    public Task StopAsync(CancellationToken cancellationToken) => throw new NotImplementedException();
}

It causes a lot of allocations and memory leaks

image

Unlike standard code:

var builder = WebApplication.CreateSlimBuilder(args);
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
builder.Services.AddHostedService<StartupService>();

var app = builder.Build();
app.Run();

public class StartupService(ILogger<StartupService> logger) : IHostedService
{
    public Task StartAsync(CancellationToken cancellationToken)
    {
        while (true) logger.LogInformation($"yeap");
    }

    public Task StopAsync(CancellationToken cancellationToken) => throw new NotImplementedException();
}

image

Where did I go wrong?

PS. .NET 8. ZLogger 2.0.0

Currently, there are no limits on buffer-queue, so if log output is too fast (while(true)! ), the memory will continue to grow because the consuming side cannot keep up.

If the standard logger is used, wait or drop when the buffer-queue limit is reached (default is wait)
While waiting, the buffer will be successfully consumed and GC will be performed.

Adding buffer options (Grow (current/default), Wait, Drop) might be a good.

In #143, we added ZLoggerOptions.FullMode.

.Block or .Drop is used can be take care of memory usage.

public enum BackgroundBufferFullMode
{
    /// <summary>
    /// Grow the queue. (default)
    /// </summary>
    Grow,

    /// <summary>
    /// Wait until there's more room in the queue.
    /// </summary>
    Block,
    
    /// <summary>
    /// Drop the overflowing log entry.
    /// </summary>
    Drop,
}

When should I expect a release?

after reviewd #145, #147, It will probably be tomorrow.

By the way, personally, I think we should opt for Grow.
The server is already at its limit, since any traffic that causes Wait would mean the application itself is failing.
As for Drop, missing logs should not be acceptable.