Is it possible to override or customize the built in logger in .net core to have a special formatted message?
For example, when doing some logging:
class A
{
private readonly ILogger<A> _logger;
public A(ILogger<A> logger)
{
_logger = logger;
}
public DoStuff()
{
_logger.LogInformation("Start of function");
}
}
The problem is, I would like to have the message end up like this:
7/4/2022 Start of function 123456789
In other words maybe some prefixes, suffixes, all in uppercase or something else. And I want ALL the logging messages to be in the same way without me building them all the time (and of course I don't want to do it using a function either like _logger.LogInformation(func("Start of function"))
).
CodePudding user response:
You can use logger factory
ILoggerFactory loggerFactory =
LoggerFactory.Create(builder =>
builder.AddSimpleConsole(options =>
{
options.IncludeScopes = true;
options.SingleLine = true;
options.TimestampFormat = "hh:mm:ss ";
options.CustomPrefix = " ~~~~~ ");
}));
There you can see more