Is there a way to format the output format in .NET Core logging?
Update: Since .NET 5 this can be customized as shown by other answers: Console log formatter documentation
Original Answer:
At the moment, this not configurable. The source code is here on GitHub:
logBuilder.Append(logName); logBuilder.Append("["); logBuilder.Append(eventId); logBuilder.AppendLine("]");
If you want that, you need to write your own logger. However you can just copy the source code of the console logger, modify as needed and change the namespaces so it doesn't interfere with the version Microsoft ships.
You can also open an issue on the logging repo to ask for this option.
As @MartinUllrich already mentioned this line break can't be disabled and you have to implement a custom logger to avoid it.
Registration:
loggerFactory.AddProvider(new CustomLoggerProvider());
The implementation (can be extended with using of the original ConsoleLogger source code - for example, you could add the GetLogLevelConsoleColors
method):
public class CustomLoggerProvider : ILoggerProvider
{
public void Dispose() { }
public ILogger CreateLogger(string categoryName)
{
return new CustomConsoleLogger(categoryName);
}
public class CustomConsoleLogger : ILogger
{
private readonly string _categoryName;
public CustomConsoleLogger(string categoryName)
{
_categoryName = categoryName;
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
if (!IsEnabled(logLevel))
{
return;
}
Console.WriteLine($"{logLevel}: {_categoryName}[{eventId.Id}]: {formatter(state, exception)}");
}
public bool IsEnabled(LogLevel logLevel)
{
return true;
}
public IDisposable BeginScope<TState>(TState state)
{
return null;
}
}
}