From 4583b5eca7d24671195ceb1ee2b22a79dc82b951 Mon Sep 17 00:00:00 2001 From: meysamhadeli Date: Sat, 10 Sep 2022 21:31:15 +0430 Subject: [PATCH] refactor logging --- Directory.Build.props | 33 ++++---- src/BuildingBlocks/HealthCheck/Extensions.cs | 2 +- src/BuildingBlocks/Logging/ElasticOptions.cs | 8 ++ src/BuildingBlocks/Logging/Extensions.cs | 78 ++++++++++++------- src/BuildingBlocks/Logging/FileOptions.cs | 8 ++ src/BuildingBlocks/Logging/LogOptions.cs | 10 +++ src/BuildingBlocks/Logging/LoggerOptions.cs | 8 -- .../Booking/src/Booking.Api/appsettings.json | 12 ++- .../Flight/src/Flight.Api/appsettings.json | 12 ++- .../src/Identity.Api/appsettings.json | 12 ++- .../src/Passenger.Api/appsettings.json | 12 ++- 11 files changed, 129 insertions(+), 66 deletions(-) create mode 100644 src/BuildingBlocks/Logging/ElasticOptions.cs create mode 100644 src/BuildingBlocks/Logging/FileOptions.cs create mode 100644 src/BuildingBlocks/Logging/LogOptions.cs delete mode 100644 src/BuildingBlocks/Logging/LoggerOptions.cs diff --git a/Directory.Build.props b/Directory.Build.props index 308f857..ec07a40 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,21 +1,12 @@ - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - + - + - + - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - diff --git a/src/BuildingBlocks/HealthCheck/Extensions.cs b/src/BuildingBlocks/HealthCheck/Extensions.cs index 7c0a691..0703d2c 100644 --- a/src/BuildingBlocks/HealthCheck/Extensions.cs +++ b/src/BuildingBlocks/HealthCheck/Extensions.cs @@ -26,7 +26,7 @@ public static class Extensions var healthChecksBuilder = services.AddHealthChecks() .AddSqlServer(sqlOptions.DefaultConnection) .AddRabbitMQ(rabbitConnectionString: $"amqp://{rabbitMqOptions.UserName}:{rabbitMqOptions.Password}@{rabbitMqOptions.HostName}") - .AddElasticsearch(logOptions.ElasticUri); + .AddElasticsearch(logOptions.Elastic.ElasticServiceUrl); if (mongoOptions.ConnectionString is not null) healthChecksBuilder.AddMongoDb(mongoOptions.ConnectionString); diff --git a/src/BuildingBlocks/Logging/ElasticOptions.cs b/src/BuildingBlocks/Logging/ElasticOptions.cs new file mode 100644 index 0000000..26fbc87 --- /dev/null +++ b/src/BuildingBlocks/Logging/ElasticOptions.cs @@ -0,0 +1,8 @@ +namespace BuildingBlocks.Logging; + +public class ElasticOptions +{ + public bool Enable { get; set; } + public string ElasticServiceUrl { get; set; } + public string ElasticSearchIndex { get; set; } +} \ No newline at end of file diff --git a/src/BuildingBlocks/Logging/Extensions.cs b/src/BuildingBlocks/Logging/Extensions.cs index b24ba8e..3f34c88 100644 --- a/src/BuildingBlocks/Logging/Extensions.cs +++ b/src/BuildingBlocks/Logging/Extensions.cs @@ -1,47 +1,67 @@ -using System.Reflection; +using System.Text; using BuildingBlocks.Web; using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Hosting; using Serilog; -using Serilog.Enrichers.Span; using Serilog.Events; using Serilog.Exceptions; using Serilog.Sinks.Elasticsearch; using Serilog.Sinks.SpectreConsole; -namespace BuildingBlocks.Logging; - -public static class Extensions +namespace BuildingBlocks.Logging { - public static WebApplicationBuilder AddCustomSerilog(this WebApplicationBuilder builder) + public static class Extensions { - builder.Host.UseSerilog((context, loggerConfiguration) => + public static WebApplicationBuilder AddCustomSerilog(this WebApplicationBuilder builder) { - var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); - var loggOptions = context.Configuration.GetSection(nameof(LogOptions)).Get(); - var appOptions = context.Configuration.GetSection(nameof(AppOptions)).Get(); + builder.Host.UseSerilog((context, services, loggerConfiguration) => + { + var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); + var logOptions = context.Configuration.GetSection(nameof(LogOptions)).Get(); + var appOptions = context.Configuration.GetSection(nameof(AppOptions)).Get(); - var logLevel = Enum.TryParse(loggOptions.Level, true, out var level) - ? level - : LogEventLevel.Information; - loggerConfiguration - .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(loggOptions.ElasticUri)) + var logLevel = Enum.TryParse(logOptions.Level, true, out var level) + ? level + : LogEventLevel.Information; + + loggerConfiguration + .MinimumLevel.Is(logLevel) + .WriteTo.SpectreConsole(logOptions.LogTemplate, logLevel) + .MinimumLevel.Override("Microsoft", LogEventLevel.Information) + // Only show ef-core information in error level + .MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Error) + // Filter out ASP.NET Core infrastructure logs that are Information and below + .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning) + .Enrich.WithExceptionDetails() + .Enrich.FromLogContext() + .ReadFrom.Configuration(context.Configuration); + + if (logOptions.Elastic.Enable) { - AutoRegisterTemplate = true, - IndexFormat = - $"{appOptions.Name}-{environment?.ToLower().Replace(".", "-")}-{DateTime.UtcNow:yyyy-MM}" - }) - .WriteTo.SpectreConsole(loggOptions.LogTemplate, logLevel) - .MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Error) - .Enrich.WithSpan() - .Enrich.WithExceptionDetails() - .Enrich.FromLogContext() - .ReadFrom.Configuration(context.Configuration); - }); + loggerConfiguration.WriteTo.Elasticsearch( + new ElasticsearchSinkOptions(new Uri(logOptions.Elastic.ElasticServiceUrl)) + { + AutoRegisterTemplate = true, + IndexFormat = + $"{appOptions.Name}-{environment?.ToLower().Replace(".", "-")}-{DateTime.UtcNow:yyyy-MM}" + }); + } - return builder; + if (logOptions.File.Enable) + { + var path = string.IsNullOrWhiteSpace(logOptions.File.Path) ? "logs/.txt" : logOptions.File.Path; + if (!Enum.TryParse(logOptions.File.Interval, true, out var interval)) + { + interval = RollingInterval.Day; + } + + loggerConfiguration.WriteTo.File(path, rollingInterval: interval, encoding: Encoding.UTF8, + outputTemplate: logOptions.LogTemplate); + } + }); + + return builder; + } } } diff --git a/src/BuildingBlocks/Logging/FileOptions.cs b/src/BuildingBlocks/Logging/FileOptions.cs new file mode 100644 index 0000000..d3021a8 --- /dev/null +++ b/src/BuildingBlocks/Logging/FileOptions.cs @@ -0,0 +1,8 @@ +namespace BuildingBlocks.Logging; + +public class FileOptions +{ + public bool Enable { get; set; } + public string Path { get; set; } + public string Interval { get; set; } +} \ No newline at end of file diff --git a/src/BuildingBlocks/Logging/LogOptions.cs b/src/BuildingBlocks/Logging/LogOptions.cs new file mode 100644 index 0000000..763ae45 --- /dev/null +++ b/src/BuildingBlocks/Logging/LogOptions.cs @@ -0,0 +1,10 @@ +namespace BuildingBlocks.Logging +{ + public class LogOptions + { + public string Level { get; set; } + public ElasticOptions Elastic { get; set; } + public FileOptions File { get; set; } + public string LogTemplate { get; set; } + } +} \ No newline at end of file diff --git a/src/BuildingBlocks/Logging/LoggerOptions.cs b/src/BuildingBlocks/Logging/LoggerOptions.cs deleted file mode 100644 index 013018e..0000000 --- a/src/BuildingBlocks/Logging/LoggerOptions.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace BuildingBlocks.Logging; - -public class LogOptions -{ - public string Level { get; set; } - public string LogTemplate { get; set; } - public string ElasticUri { get; set; } -} diff --git a/src/Services/Booking/src/Booking.Api/appsettings.json b/src/Services/Booking/src/Booking.Api/appsettings.json index f5fb364..e300ef0 100644 --- a/src/Services/Booking/src/Booking.Api/appsettings.json +++ b/src/Services/Booking/src/Booking.Api/appsettings.json @@ -3,9 +3,17 @@ "Name": "Booking-Service" }, "LogOptions": { - "Level": "Information", + "Level": "information", "LogTemplate": "{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}", - "ElasticUri": "http://localhost:9200" + "Elastic": { + "Enable": true, + "ElasticServiceUrl": "http://localhost:9200" + }, + "File": { + "enable": false, + "path": "logs/logs.txt", + "interval": "day" + } }, "Jwt": { "Authority": "https://localhost:5005", diff --git a/src/Services/Flight/src/Flight.Api/appsettings.json b/src/Services/Flight/src/Flight.Api/appsettings.json index eabc956..d32d545 100644 --- a/src/Services/Flight/src/Flight.Api/appsettings.json +++ b/src/Services/Flight/src/Flight.Api/appsettings.json @@ -3,9 +3,17 @@ "Name": "Flight-Service" }, "LogOptions": { - "Level": "Information", + "Level": "information", "LogTemplate": "{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}", - "ElasticUri": "http://localhost:9200" + "Elastic": { + "Enable": true, + "ElasticServiceUrl": "http://localhost:9200" + }, + "File": { + "enable": false, + "path": "logs/logs.txt", + "interval": "day" + } }, "ConnectionStrings": { "DefaultConnection": "Server=.\\sqlexpress;Database=FlightDB;Trusted_Connection=True;MultipleActiveResultSets=true" diff --git a/src/Services/Identity/src/Identity.Api/appsettings.json b/src/Services/Identity/src/Identity.Api/appsettings.json index f2fc143..e687693 100644 --- a/src/Services/Identity/src/Identity.Api/appsettings.json +++ b/src/Services/Identity/src/Identity.Api/appsettings.json @@ -12,9 +12,17 @@ "Password": "guest" }, "LogOptions": { - "Level": "Information", + "Level": "information", "LogTemplate": "{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}", - "ElasticUri": "http://localhost:9200" + "Elastic": { + "Enable": true, + "ElasticServiceUrl": "http://localhost:9200" + }, + "File": { + "enable": false, + "path": "logs/logs.txt", + "interval": "day" + } }, "PersistMessageOptions": { "Interval": 30, diff --git a/src/Services/Passenger/src/Passenger.Api/appsettings.json b/src/Services/Passenger/src/Passenger.Api/appsettings.json index 84afe9f..ed468a5 100644 --- a/src/Services/Passenger/src/Passenger.Api/appsettings.json +++ b/src/Services/Passenger/src/Passenger.Api/appsettings.json @@ -20,9 +20,17 @@ "Password": "guest" }, "LogOptions": { - "Level": "Information", + "Level": "information", "LogTemplate": "{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}", - "ElasticUri": "http://localhost:9200" + "Elastic": { + "Enable": true, + "ElasticServiceUrl": "http://localhost:9200" + }, + "File": { + "enable": false, + "path": "logs/logs.txt", + "interval": "day" + } }, "PersistMessageOptions": { "Interval": 30,