I am using below code for accessing the Serilog for non Host based Console application..I have two requirement to achieve.
I should be able to access MS extension Logger in all the other classes once Serilog is initialized. We should be able to see the logging statement after each logging line executed.. I am using below code.
Program.cs
private static void main()
{
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
Log.Logger.Information("hello"); //it's not printing the hello in console window.
}
appsettings.json
"Serilog": {
"MinimumLevel": "Verbose",
"WriteTo": [
{
"Name": "Async",
"Args": {
"configure": [
{
"Name": "Console",
"Args": {
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:j}{NewLine}{Properties:j}{NewLine}{Exception}"
}
},
{
"Name": "File",
"Args": {
"restrictedToMinimumLevel": "Warning",
"path": "Logs\\log.txt",
"rollingInterval": "Day",
"fileSizeLimitBytes": 10240,
"rollOnFileSizeLimit": true,
"retainedFileCountLimit": 30
}
}
]
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithExceptionDetails" ],
"Properties": {
"ApplicationName": "SampleApp",
"Environment": "Int"
}
}
}```