adams85 / filelogger

A lightweight yet feature-rich file logger implementation for the Microsoft.Extensions.Logging framework.
MIT License
147 stars 22 forks source link

Counter Start value setting #6

Closed GGY-CM closed 3 years ago

GGY-CM commented 4 years ago

Hi,

Can you add a setting to allow set the start value of the counter? or set its start value to 1 instead of 0?

Thanks,

adams85 commented 4 years ago

You can achieve this pretty easy by subclassing FileLoggerProcessor and overriding the GetCounter virtual method.

I put together a sample console app for you to demonstrate how to do this.

However, I'll consider introducing some hook to control this from the settings. A CounterFormatter callback or something.

GGY-CM commented 4 years ago

Thank you very much.

adams85 commented 3 years ago

Version 3.2.0 introduces the previously mentioned hook mechanism. Now this is possible through configuration, without subclassing anything:

builder.AddFile(o =>
{
    o.RootPath = AppContext.BaseDirectory;

    o.PathPlaceholderResolver =  (placeholderName, inlineFormat, context) =>
        placeholderName == "counter" ? 
        (context.Counter + 1).ToString(inlineFormat ?? context.CounterFormat, CultureInfo.InvariantCulture) :
        null;
});