I use Serilog to record logscope follow this document, but it not work.
If I configuration the outputTemplate's value with {Properites}, can record the logscope but it also record all the event properties:
"Serilog": {
"WriteTo": [
{
"Name": "File",
"Args": {
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{Properties}{NewLine}{Exception}"
}
}
]
}
output:
[22:35:03 INF] This is a log message{SourceContext="APIs.Controllers.HomeController", name="eason", ActionId="45b8843d-dd51-4f2b-bb7b-1e12a082b29a", ActionName="APIs.Controllers.HomeController.Health (APIs)", RequestId="0HMJ4RJFPK2CL:00000002", RequestPath="/Home", ConnectionId="0HMJ4RJFPK2CL", Scope=["This is a log scope, name:eason"]}
[22:35:03 INF] This is another log message{SourceContext="APIs.Controllers.HomeController", name="eason", ActionId="45b8843d-dd51-4f2b-bb7b-1e12a082b29a", ActionName="APIs.Controllers.HomeController.Health (APIs)", RequestId="0HMJ4RJFPK2CL:00000002", RequestPath="/Home", ConnectionId="0HMJ4RJFPK2CL", Scope=["This is a log scope, name:eason"]}
I want to out log message like this :
[22:35:03 INF] This is a log message{Scope=["This is a log scope, name:eason"]}
[22:35:03 INF] This is another log message{Scope=["This is a log scope, name:eason"]}
How do I do?
I use ASP.NET Core 6 with Serilog.AspNetCore 5.0.0
builder.Logging.ClearProviders();
builder.Host.UseSerilog((context, cfg) =>
{
cfg.ReadFrom.Configuration(context.Configuration);
// doesn't work
// cfg.Enrich.FromLogContext();
});
record log:
using var scope = _logger.BeginScope("This is a log scope, name:{name}", "eason");
_logger.LogInformation("This is a log message");
_logger.LogInformation("This is another log message");
Configuration:
"Serilog": {
"Using": [
"Serilog.Sinks.Console",
"Serilog.Sinks.File"
],
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "",
"encoding": "System.Text.Encoding::UTF8",
"rollOnFileSizeLimit": true,
"fileSizeLimitBytes": 2097152,
"retainedFileCountLimit": 10,
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{Properties}{NewLine}{Exception}"
}
}
]
}
reference:
Formatting Output