adams85 / filelogger

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

Disable category name from logging - clean output #13

Closed eusebiu closed 3 years ago

eusebiu commented 3 years ago

Would be nice to have the possibility to clean the output of the category name:

https://stackoverflow.com/questions/55924730/is-it-possible-to-disable-category-output-in-net-core-consolelogger-and-debuglo

Thanks!

adams85 commented 3 years ago

This has been possible since the very beginning. The IFileLogEntryTextBuilder interface provides full control over log message formatting. However, you're better off subclassing the default implementation of this interface (FileLogEntryTextBuilder). E.g.:

class CleanFileLogEntryTextBuilder : FileLogEntryTextBuilder
{
    protected override int MessagePaddingWidth => 0;

    public override void BuildEntryText(StringBuilder sb, string categoryName, LogLevel logLevel, EventId eventId, string message, Exception exception, IExternalScopeProvider scopeProvider, DateTimeOffset timestamp)
    {
        if (!string.IsNullOrEmpty(message))
            AppendMessage(sb, message);

        if (exception != null)
            AppendException(sb, exception);
    }
}

Of course, you need to tell the library to use your custom formatter. One way to do this:

builder.AddFile(o =>
{
    o.TextBuilder = new CleanFileLogEntryTextBuilder();
    // ...rest of configuration is ommitted for brevity...
});
eusebiu commented 3 years ago

Thanks! Works fine!