Using (.Net 6.0) I am trying to output a few additional default information with all log When I output to console I don't see the properties, however if I enable Json and output to file it all works, wondering if you have any clues?
public class Program
{
public static int Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
Log.Logger = new LoggerConfiguration()
//.WriteTo.File(new Serilog.Formatting.Json.JsonFormatter(), "Logs/log-.txt", fileSizeLimitBytes: 5242880, rollOnFileSizeLimit: true, rollingInterval: RollingInterval.Day, shared: true)
.Enrich.FromLogContext()
.Enrich.WithProcessId()
.Enrich.WithProcessName()
.Enrich.WithProperty("Prop1", "ABC123")
.Enrich.WithProperty("Prop2", "1.00")
.Enrich.WithProperty("PID", Process.GetCurrentProcess().Id)
.WriteTo.Console()
.CreateLogger();
builder.Host.UseSerilog();
// Add services to the container.
var app = builder.Build();
// Configure the HTTP request pipeline.
app.MapGet("/weatherforecast", () =>
{
Log.Information("Hello....");
return forecast;
});
try
{
app.Run();
}
catch (Exception ex)
{
ex.ToString();
}
finally
{
Log.CloseAndFlush();
}
return 0;
}
}
You have to include these properties into the template of the Console
sink.
The placeholders match by name; for the example given, use {Prop1}, {Prop2} and {PID}
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.Enrich.WithProcessId()
.Enrich.WithProcessName()
.Enrich.WithProperty("Prop1", "ABC123")
.Enrich.WithProperty("Prop2", "1.00")
.Enrich.WithProperty("PID", Process.GetCurrentProcess().Id) // 9840
.WriteTo.Console(
outputTemplate: "{Timestamp:HH:mm:ss} {Level:u3} {Message} {Prop1} {Prop2} {PID}{NewLine}{Exception}"
)
.CreateLogger();
Output:
23:10:40 INF Hello.... ABC123 1.00 9840