I want to log debug messages only to output window, smth like Debug.WriteLine. Put it simply -- emulate Debug.WriteLine via serilog, so output window should only log debug level, other sinks should ignore it. Is it possible to achieve? Is it possible to achieve via json configuration? Given following simple config, what should I add to achieve this:
{
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Warning",
"Microsoft.Hosting": "Information",
"System": "Warning",
"Serilog.AspNetCore": "Warning"
}
},
"Filter": [],
"WriteTo": [
{
"Name": "Debug",
"Args": {
"outputTemplate": "{Timestamp:HH:mm:ss.fff} [{ProcessId}:{ThreadId,-2}] [{Level:u3}] [{SourceContext}] {Message:lj}{NewLine}{Exception}"
}
},
{
"Name": "Console",
"Args": {
"restrictedToMinimumLevel": "Information",
"outputTemplate": "{Timestamp:HH:mm:ss.fff} [{ProcessId}:{ThreadId,-2}] [{Level:u3}] [{SourceContext}] {Message:lj}{NewLine}{Exception}"
}
}
],
"Enrich": [
"FromLogContext",
"WithExceptionDetails",
"WithDefaultDestructurers",
"WithProcessId",
"WithThreadId",
"WithMachineName",
"WithEnvironmentUserName"
]
}
}
?
Thanks in advance.
To start logging your output to Serilog, you need to add something like..
Serilog.Log.Logger.Debug("*************************** Read DailyFile Started *****************************");
To filter your logs you can either mentioned into your LoggerConfiguration() or you can mentioned into Appsettings.json. You can filter your log based on LogLevel or EventType or any other custom properties.
Please make sure following packages are installed.
Program.cs
var configFile = new ConfigurationBuilder().SetBasePath(Directory.GetParent(AppContext.BaseDirectory).FullName)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.ReadFrom.Configuration(configFile)
.WriteTo.Console()
//.WriteTo.Debug()
.WriteTo.Logger(lc => lc
.Filter.ByIncludingOnly(l => l.Level == LogEventLevel.Debug)
.WriteTo.Debug())
.WriteTo.Logger(lc => lc
.Filter.ByExcluding(l => l.Level == LogEventLevel.Information || l.Level == LogEventLevel.Debug || l.Level == LogEventLevel.Warning || l.Properties["EventType"].Equals("System"))
.WriteTo.File(Directory.GetParent(AppContext.BaseDirectory).FullName + @"\\Logs\\log-Information-.txt", rollingInterval: RollingInterval.Day))
.WriteTo.Logger(lc => lc
.Filter.ByIncludingOnly(l => l.Level == LogEventLevel.Error || l.Level == LogEventLevel.Fatal)
.WriteTo.File(Directory.GetParent(AppContext.BaseDirectory).FullName + @"\\Logs\\log-Error-.txt", rollingInterval: RollingInterval.Day))
.CreateLogger();
Serilog.Log.Logger.Debug("*************************** Read DailyFile Started *****************************");
Appsettings.json
"Serilog": {
"Using": [ "Serilog.Sinks.MSSqlServer", "Serilog.Sinks.Console", "Serilog.Sinks.Debug", "Serilog.Sinks.File", "Serilog.Enrichers.Environment", "Serilog.Enrichers.Thread", "Serilog.Exceptions", "Serilog.Expressions" ],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "Console",
"Args": {
"outputTemplate": "[{Timestamp:yyyy:MM:dd hh:mm:ss} {CorrelationId} {Level:u3}] {Message:lj}{NewLine}{Exception}",
"restrictedToMinimumLevel": "Debug"
}
},
{
"Name": "Debug",
"Args": {
"outputTemplate": "[{Timestamp:yyyy:MM:dd hh:mm:ss} {CorrelationId} {Level:u3}] {Message:lj}{NewLine}{Exception}",
"restrictedToMinimumLevel": "Debug",
"filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "Level = 'Debug'" //"StartsWith(SourceContext,'Microsoft.EntityFrameworkCore.ChangeTracking')",
}
}
]
}
},
}
Output windows on VS: