How do I configure the dotnet Console logger to not emit the log level?

catmanjan 61 Reputation points
2025-02-28T00:50:57.86+00:00

I am using the stock standard console logger, however it is unuseful for me to have info: at the start of each line - how can I remove this via config?

2025-02-28T00:02:10.553Z info: Scope.Scope.Scope[0] blah blah blah

Here is my current appsettings.json:

{
  "Logging": {
    "Console": {
      "FormatterName": "simple",
      "FormatterOptions": {
        "SingleLine": true,
        "TimestampFormat": "yyyy-MM-ddTHH:MM:ss.fffZ ",
        "UseUtcTimestamp": true,
        "JsonWriterOptions": {
          "Indented": true
        },
        "IncludeScopes": false
      }
    }
  }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,317 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 49,051 Reputation points Microsoft Vendor
    2025-02-28T02:12:21.83+00:00

    Hi @catmanjan , Welcome to Microsoft Q&A,

    You can remove the info: prefix from the console log output via a custom log formatter. The commonly used simple formatter does not allow you to directly remove the log level prefix.

    using System;
    using System.IO;
    using Microsoft.Extensions.Logging;
    using Microsoft.Extensions.Logging.Console;
    
    public class CustomConsoleFormatter : ConsoleFormatter
    {
        public CustomConsoleFormatter() : base("custom")
        {
        }
    
        public override void Write<TState>(in LogEntry<TState> logEntry, IExternalScopeProvider scopeProvider, TextWriter textWriter)
        {
            var timestamp = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
            textWriter.WriteLine($"{timestamp} {logEntry.State}");
        }
    }
    
    

    Add the formatter in Program.cs

    builder.Logging.AddConsoleFormatter<CustomConsoleFormatter, ConsoleFormatterOptions>();
    

    Edit appsettings.json

    {
      "Logging": {
        "Console": {
          "FormatterName": "custom"
        }
      }
    }
    

    You can also check out the official documentation examples: Logging in C# and .NET

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    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.