From 3624e5b86de2a5a8227818c8a2a5b18100f6425e Mon Sep 17 00:00:00 2001 From: "kshitij.k" Date: Wed, 6 Mar 2024 19:42:50 +0530 Subject: [PATCH 1/4] feat: add serilog logging --- .../DotnetFoundation.Api.csproj | 2 ++ .../DotnetFoundation.Api/Program.cs | 8 ++++++ .../DotnetFoundation.Api/appsettings.json | 25 +++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/DotnetFoundation/DotnetFoundation.Api/DotnetFoundation.Api.csproj b/DotnetFoundation/DotnetFoundation.Api/DotnetFoundation.Api.csproj index 6285449..069f46e 100644 --- a/DotnetFoundation/DotnetFoundation.Api/DotnetFoundation.Api.csproj +++ b/DotnetFoundation/DotnetFoundation.Api/DotnetFoundation.Api.csproj @@ -8,6 +8,8 @@ + + diff --git a/DotnetFoundation/DotnetFoundation.Api/Program.cs b/DotnetFoundation/DotnetFoundation.Api/Program.cs index 118237c..b9df597 100644 --- a/DotnetFoundation/DotnetFoundation.Api/Program.cs +++ b/DotnetFoundation/DotnetFoundation.Api/Program.cs @@ -4,6 +4,7 @@ using DotnetFoundation.Services; using Microsoft.AspNetCore.Mvc; using Microsoft.OpenApi.Models; +using Serilog; using Swashbuckle.AspNetCore.Filters; using System.Reflection; using System.Text.Json.Serialization; @@ -52,6 +53,13 @@ options.IncludeXmlComments(filePath); }); +// logging using serilog +Log.Logger = new LoggerConfiguration() + .ReadFrom.Configuration(builder.Configuration) + .CreateLogger(); +Log.Logger.Information("Logging has started"); +builder.Host.UseSerilog(); + // Adding HTTP Context builder.Services.AddHttpContextAccessor(); diff --git a/DotnetFoundation/DotnetFoundation.Api/appsettings.json b/DotnetFoundation/DotnetFoundation.Api/appsettings.json index 76acacb..32d0275 100644 --- a/DotnetFoundation/DotnetFoundation.Api/appsettings.json +++ b/DotnetFoundation/DotnetFoundation.Api/appsettings.json @@ -9,6 +9,31 @@ "Microsoft.AspNetCore": "Warning" } }, + "Serilog": { + "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ], + "MinimumLevel": { + "Default": "Information" + }, + "WriteTo": [ + { + "Name": "Console", + "Args": { + "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact", + "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {NewLine}{Exception}\n" + } + }, + { + "Name": "File", + "Args": { + "path": "./Logs/log-.ndjson", + "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact", + "outputTemplate": "{\t\"timestamp\":\"{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}\", \"level\":\"{Level}\", \"severity\":\"{Level:u3}\", \"tracebackId\":\"{NewGuid}\", \"source\":\"{SourceContext}\", \"data\":\"{PropertiesToJson}\", \"message\":\"{Message:lj}\", \"stackTrace\":\"{Exception}\"}\n\n", + "rollingInterval": "Day" + } + } + ], + "Enrich": [ "WithThreadId", "WithProcessId", "WithEnvironmentName" ] + }, "ConnectionStrings": { "DBConnection": "server=foundation-mariadb;Port=3306;database=DotnetFoundation;Uid=root;pwd=Change123" }, From 25bbabc2a6263be3dff89e2d864f7e199e01e5b3 Mon Sep 17 00:00:00 2001 From: "kshitij.k" Date: Mon, 11 Mar 2024 13:56:33 +0530 Subject: [PATCH 2/4] feat: move serilog to dev.json - WIP --- .../DotnetFoundation.Api/Program.cs | 2 ++ .../appsettings.Development.json | 30 ++++++++++++++++--- .../DotnetFoundation.Api/appsettings.json | 25 ---------------- 3 files changed, 28 insertions(+), 29 deletions(-) diff --git a/DotnetFoundation/DotnetFoundation.Api/Program.cs b/DotnetFoundation/DotnetFoundation.Api/Program.cs index b9df597..0c9a86a 100644 --- a/DotnetFoundation/DotnetFoundation.Api/Program.cs +++ b/DotnetFoundation/DotnetFoundation.Api/Program.cs @@ -82,6 +82,8 @@ app.UseHttpsRedirection(); +app.UseSerilogRequestLogging(); + app.UseAuthentication(); app.UseAuthorization(); diff --git a/DotnetFoundation/DotnetFoundation.Api/appsettings.Development.json b/DotnetFoundation/DotnetFoundation.Api/appsettings.Development.json index 0c208ae..5f6b841 100644 --- a/DotnetFoundation/DotnetFoundation.Api/appsettings.Development.json +++ b/DotnetFoundation/DotnetFoundation.Api/appsettings.Development.json @@ -1,8 +1,30 @@ { - "Logging": { - "LogLevel": { + "Serilog": { + "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ], + "MinimumLevel": { "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } + "Override": { + "Microsoft": "Information" + } + }, + "WriteTo": [ + { + "Name": "Console", + "Args": { + "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact", + "outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3}] {Message:lj} {NewLine}{Exception}\n" + } + }, + { + "Name": "File", + "Args": { + "path": "./Logs/log-.ndjson", + "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact", + "outputTemplate": "{\t\"timestamp\":\"{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}\", \"level\":\"{Level}\", \"severity\":\"{Level:u3}\", \"RequestMethod\":\"{RequestMethod}\", \"RequestPath\":\"{RequestPath}\", \"tracebackId\":\"{EndpointName}\", \"source\":\"{SourceContext}\", \"data\":\"{PropertiesToJson}\", \"message\":\"{@Message:lj}\", \"stackTrace\":\"{@Exception}\"}\n\n", + "rollingInterval": "Day" + } + } + ], + "Enrich": [ "FromLogContext", "WithThreadId", "WithProcessId", "WithEnvironmentName" ] } } diff --git a/DotnetFoundation/DotnetFoundation.Api/appsettings.json b/DotnetFoundation/DotnetFoundation.Api/appsettings.json index 32d0275..76acacb 100644 --- a/DotnetFoundation/DotnetFoundation.Api/appsettings.json +++ b/DotnetFoundation/DotnetFoundation.Api/appsettings.json @@ -9,31 +9,6 @@ "Microsoft.AspNetCore": "Warning" } }, - "Serilog": { - "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ], - "MinimumLevel": { - "Default": "Information" - }, - "WriteTo": [ - { - "Name": "Console", - "Args": { - "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact", - "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {NewLine}{Exception}\n" - } - }, - { - "Name": "File", - "Args": { - "path": "./Logs/log-.ndjson", - "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact", - "outputTemplate": "{\t\"timestamp\":\"{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}\", \"level\":\"{Level}\", \"severity\":\"{Level:u3}\", \"tracebackId\":\"{NewGuid}\", \"source\":\"{SourceContext}\", \"data\":\"{PropertiesToJson}\", \"message\":\"{Message:lj}\", \"stackTrace\":\"{Exception}\"}\n\n", - "rollingInterval": "Day" - } - } - ], - "Enrich": [ "WithThreadId", "WithProcessId", "WithEnvironmentName" ] - }, "ConnectionStrings": { "DBConnection": "server=foundation-mariadb;Port=3306;database=DotnetFoundation;Uid=root;pwd=Change123" }, From 072ecbb456958e2fee0ef6b5e3dd33c070bcaf7e Mon Sep 17 00:00:00 2001 From: "kshitij.k" Date: Fri, 22 Mar 2024 09:26:03 +0530 Subject: [PATCH 3/4] feat: add default serilog formatting --- .../DotnetFoundation.Api/DotnetFoundation.Api.csproj | 1 + DotnetFoundation/DotnetFoundation.Api/Program.cs | 4 +--- .../DotnetFoundation.Api/appsettings.Development.json | 6 +++--- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/DotnetFoundation/DotnetFoundation.Api/DotnetFoundation.Api.csproj b/DotnetFoundation/DotnetFoundation.Api/DotnetFoundation.Api.csproj index 069f46e..7d50042 100644 --- a/DotnetFoundation/DotnetFoundation.Api/DotnetFoundation.Api.csproj +++ b/DotnetFoundation/DotnetFoundation.Api/DotnetFoundation.Api.csproj @@ -10,6 +10,7 @@ + diff --git a/DotnetFoundation/DotnetFoundation.Api/Program.cs b/DotnetFoundation/DotnetFoundation.Api/Program.cs index 0c9a86a..abda602 100644 --- a/DotnetFoundation/DotnetFoundation.Api/Program.cs +++ b/DotnetFoundation/DotnetFoundation.Api/Program.cs @@ -54,9 +54,7 @@ }); // logging using serilog -Log.Logger = new LoggerConfiguration() - .ReadFrom.Configuration(builder.Configuration) - .CreateLogger(); +Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(builder.Configuration).CreateLogger(); Log.Logger.Information("Logging has started"); builder.Host.UseSerilog(); diff --git a/DotnetFoundation/DotnetFoundation.Api/appsettings.Development.json b/DotnetFoundation/DotnetFoundation.Api/appsettings.Development.json index 5f6b841..c20f7c3 100644 --- a/DotnetFoundation/DotnetFoundation.Api/appsettings.Development.json +++ b/DotnetFoundation/DotnetFoundation.Api/appsettings.Development.json @@ -11,8 +11,8 @@ { "Name": "Console", "Args": { - "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact", - "outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3}] {Message:lj} {NewLine}{Exception}\n" + "formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact", + "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss zzz} [{Level:u3}] {Message:lj} {NewLine}{Exception}\n" } }, { @@ -20,7 +20,7 @@ "Args": { "path": "./Logs/log-.ndjson", "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact", - "outputTemplate": "{\t\"timestamp\":\"{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}\", \"level\":\"{Level}\", \"severity\":\"{Level:u3}\", \"RequestMethod\":\"{RequestMethod}\", \"RequestPath\":\"{RequestPath}\", \"tracebackId\":\"{EndpointName}\", \"source\":\"{SourceContext}\", \"data\":\"{PropertiesToJson}\", \"message\":\"{@Message:lj}\", \"stackTrace\":\"{@Exception}\"}\n\n", + // "outputTemplate": "{\t\"timestamp\":\"{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}\", \"level\":\"{Level}\", \"severity\":\"{Level:u3}\", \"tracebackId\":\"{EndpointName}\", \"requestId\":\"{RequestId}\", \"httpMethod\":\"{RequestMethod}\", \"requestUrl\":\"{RequestPath}\", \"StatusCode\":\"{StatusCode}\", \"source\":\"{SourceContext}\", \"message\":\"{Message:lj}\", \"stackTrace\":\"{Exception:lj}\"}\n\n", "rollingInterval": "Day" } } From f1651f548e314176515bf453eeffc0d23e221b9b Mon Sep 17 00:00:00 2001 From: "kshitij.k" Date: Tue, 26 Mar 2024 20:46:30 +0530 Subject: [PATCH 4/4] feat: add serilog expressions --- .../DotnetFoundation.Api.csproj | 2 +- .../DotnetFoundation.Api/Program.cs | 13 +++++++- .../appsettings.Development.json | 32 +++---------------- 3 files changed, 18 insertions(+), 29 deletions(-) diff --git a/DotnetFoundation/DotnetFoundation.Api/DotnetFoundation.Api.csproj b/DotnetFoundation/DotnetFoundation.Api/DotnetFoundation.Api.csproj index 7d50042..ad078ef 100644 --- a/DotnetFoundation/DotnetFoundation.Api/DotnetFoundation.Api.csproj +++ b/DotnetFoundation/DotnetFoundation.Api/DotnetFoundation.Api.csproj @@ -9,8 +9,8 @@ + - diff --git a/DotnetFoundation/DotnetFoundation.Api/Program.cs b/DotnetFoundation/DotnetFoundation.Api/Program.cs index abda602..8a99f72 100644 --- a/DotnetFoundation/DotnetFoundation.Api/Program.cs +++ b/DotnetFoundation/DotnetFoundation.Api/Program.cs @@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.OpenApi.Models; using Serilog; +using Serilog.Templates; using Swashbuckle.AspNetCore.Filters; using System.Reflection; using System.Text.Json.Serialization; @@ -54,7 +55,17 @@ }); // logging using serilog -Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(builder.Configuration).CreateLogger(); +Log.Logger = new LoggerConfiguration() + .WriteTo.File( + formatter: new ExpressionTemplate( + "{ {Timestamp: @t, Level: @l, TracebackId: @tr, RequestMethod, RequestPath, StatusCode, SourceContext, Data:{EventId: @i, RequestId}, Message: @m, Exception: @x} }\n\n" + ), + path: "logs/log-.ndjson", + rollingInterval: RollingInterval.Day) + .WriteTo.Console( + outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss zzz} [{Level:u3}] {Message:lj} {NewLine}{Exception}\n") + .CreateLogger(); + Log.Logger.Information("Logging has started"); builder.Host.UseSerilog(); diff --git a/DotnetFoundation/DotnetFoundation.Api/appsettings.Development.json b/DotnetFoundation/DotnetFoundation.Api/appsettings.Development.json index c20f7c3..1b2d3ba 100644 --- a/DotnetFoundation/DotnetFoundation.Api/appsettings.Development.json +++ b/DotnetFoundation/DotnetFoundation.Api/appsettings.Development.json @@ -1,30 +1,8 @@ { - "Serilog": { - "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ], - "MinimumLevel": { + "Logging": { + "LogLevel": { "Default": "Information", - "Override": { - "Microsoft": "Information" - } - }, - "WriteTo": [ - { - "Name": "Console", - "Args": { - "formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact", - "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss zzz} [{Level:u3}] {Message:lj} {NewLine}{Exception}\n" - } - }, - { - "Name": "File", - "Args": { - "path": "./Logs/log-.ndjson", - "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact", - // "outputTemplate": "{\t\"timestamp\":\"{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}\", \"level\":\"{Level}\", \"severity\":\"{Level:u3}\", \"tracebackId\":\"{EndpointName}\", \"requestId\":\"{RequestId}\", \"httpMethod\":\"{RequestMethod}\", \"requestUrl\":\"{RequestPath}\", \"StatusCode\":\"{StatusCode}\", \"source\":\"{SourceContext}\", \"message\":\"{Message:lj}\", \"stackTrace\":\"{Exception:lj}\"}\n\n", - "rollingInterval": "Day" - } - } - ], - "Enrich": [ "FromLogContext", "WithThreadId", "WithProcessId", "WithEnvironmentName" ] + "Microsoft.AspNetCore": "Warning" + } } -} +} \ No newline at end of file