Figured out the basics of Serilog. Now trying to add a few enrichers so I can print username or machinename or classname, etc in the every log lines.
This is the code I have so far,
using System;
using Serilog;
using Serilog.Sinks.SystemConsole;
using Serilog.Sinks.File;
using Serilog.Enrichers;
using Serilog.Core;
using Serilog.Events;
using System.Threading;
using Serilog.Context;
var outputTemplate = "{MachineName} | {UserName} | {Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u4}] | {ClassName} | {Message:l}{NewLine}{Exception}";
var Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console(outputTemplate: outputTemplate)
.WriteTo.File("logFile-.log",
outputTemplate: outputTemplate)
.Enrich.FromLogContext()
.Enrich.WithEnvironmentUserName()
.Enrich.WithProperty("Version", "1.0.0")
.CreateLogger();
Logger.Information("Hello, Serilog!");
var position = new { Latitude = 25, Longitude = 134 };
var elapsedMs = 35;
for (int i = 0; i < 5; i++)
{
Logger.Information("Processed {@position} in {elapsed} ms.", position, elapsedMs);
Logger.Information("");
}
using (LogContext.PushProperty("SourceContext", "TestClass"))
{
for (int i = 0; i < 5; i++)
{
Logger.Information("Processed {@position} in {elapsed} ms.", position, elapsedMs);
Logger.Information("");
}
}
Output
| | 2018-03-03 19:02:56.247 -05:00 [INFO] | | Hello, Serilog!
| | 2018-03-03 19:02:56.287 -05:00 [INFO] | | Processed {Latitude=25, Longitude=134} in 35 ms.
| | 2018-03-03 19:02:56.295 -05:00 [INFO] | |
| | 2018-03-03 19:02:56.295 -05:00 [INFO] | | Processed {Latitude=25, Longitude=134} in 35 ms.
| | 2018-03-03 19:02:56.296 -05:00 [INFO] | |
| | 2018-03-03 19:02:56.297 -05:00 [INFO] | | Processed {Latitude=25, Longitude=134} in 35 ms.
| | 2018-03-03 19:02:56.298 -05:00 [INFO] | |
| | 2018-03-03 19:02:56.298 -05:00 [INFO] | | Processed {Latitude=25, Longitude=134} in 35 ms.
| | 2018-03-03 19:02:56.299 -05:00 [INFO] | |
| | 2018-03-03 19:02:56.300 -05:00 [INFO] | | Processed {Latitude=25, Longitude=134} in 35 ms.
| | 2018-03-03 19:02:56.301 -05:00 [INFO] | |
| | 2018-03-03 19:02:56.302 -05:00 [INFO] | TestClass | Processed {Latitude=25, Longitude=134} in 35 ms.
| | 2018-03-03 19:02:56.307 -05:00 [INFO] | TestClass |
| | 2018-03-03 19:02:56.308 -05:00 [INFO] | TestClass | Processed {Latitude=25, Longitude=134} in 35 ms.
| | 2018-03-03 19:02:56.310 -05:00 [INFO] | TestClass |
| | 2018-03-03 19:02:56.310 -05:00 [INFO] | TestClass | Processed {Latitude=25, Longitude=134} in 35 ms.
| | 2018-03-03 19:02:56.311 -05:00 [INFO] | TestClass |
| | 2018-03-03 19:02:56.312 -05:00 [INFO] | TestClass | Processed {Latitude=25, Longitude=134} in 35 ms.
| | 2018-03-03 19:02:56.313 -05:00 [INFO] | TestClass |
| | 2018-03-03 19:02:56.316 -05:00 [INFO] | TestClass | Processed {Latitude=25, Longitude=134} in 35 ms.
| | 2018-03-03 19:02:56.317 -05:00 [INFO] | TestClass |
Press any key to continue . . .
From my understanding, I have to manually define the "placeholders" (MachineName
or UserName
or ClassName
) in outputTemplate
while creating the Logger
object. I am not sure how to make these placeholders optional (i.e. without printing blank space in log line) if I don't need to use them. (I would not like to print class name for few lines for examples). Or maybe I don't understand the concept of Enrichers properly.
Any help is greatly appreciated!!