.NET Core - Removing the type name from logging output

Eusebiu Marcu 201 Reputation points
2021-01-18T12:25:43.217+00:00

How can I remove the type name from the log output (see below the MyApp.App[0]) and not only on the console? Tried various settings from ILogger API with no luck.

info: MyApp.App[0] @ 2021-01-18T13:27:57.5573387+01:00
My test message...

to simply become

info: My test message...

Developer technologies | .NET | .NET Runtime
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. David Strickland 6 Reputation points
    2022-07-06T04:46:11.517+00:00

    Gotta love the guys that would rather pick at the question than answer it ... sigh. In case anyone else needs this

          public static class ConsoleLoggerExtensions  
         {  
             public static ILoggingBuilder AddCustomFormatter(  
                 this ILoggingBuilder builder) =>  
                 builder.AddConsole(options => options.FormatterName = nameof(CustomLoggingFormatter))  
                     .AddConsoleFormatter<CustomLoggingFormatter, ConsoleFormatterOptions>();  
         }  
         public sealed class CustomLoggingFormatter : ConsoleFormatter, IDisposable  
         {  
             private readonly IDisposable _optionsReloadToken;  
             private ConsoleFormatterOptions _formatterOptions;  
             public CustomLoggingFormatter(IOptionsMonitor<ConsoleFormatterOptions> options)  
         // Case insensitive  
         : base(nameof(CustomLoggingFormatter)) =>  
         (_optionsReloadToken, _formatterOptions) =  
             (options.OnChange(ReloadLoggerOptions), options.CurrentValue);  
             private void ReloadLoggerOptions(ConsoleFormatterOptions options) =>  
         _formatterOptions = options;  
       
             public override void Write<TState>(  
         in LogEntry<TState> logEntry,  
         IExternalScopeProvider scopeProvider,  
         TextWriter textWriter)  
             {  
                 string? message =  
                     logEntry.Formatter?.Invoke(  
                         logEntry.State, logEntry.Exception);  
       
                 if (message is null)  
                 {  
                     return;  
                 }  
       
                 textWriter.WriteLine($"{message}");  
             }  
             public void Dispose() => _optionsReloadToken?.Dispose();  
         }  
      
    

    then in .ConfigureServices(services =>

                services.AddLogging(opt =>  
                {  
                    opt.AddCustomFormatter();  
                }  
    
    1 person found this answer helpful.

  2. Eusebiu Marcu 201 Reputation points
    2021-01-18T14:18:06.347+00:00
    0 comments No comments

  3. Jerry Cai-MSFT 991 Reputation points
    2021-01-19T03:03:05.88+00:00

    Hi,EusebiuMarcu

    Could you please tell me why you want to show the log without the category? the log is used to determine whether the code can work or not and then locate

    the error. You need the information of the category to check where the problem lies. If you really want to delete it ,then you can check this link to custom the

    logger:

    custom-logging-provider

    Best Regards,
    Jerry Cai


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.