From ccb7ffa03579587e4be93d260a7548ace2f6f5e0 Mon Sep 17 00:00:00 2001 From: meysamhadeli Date: Wed, 25 May 2022 01:39:40 +0430 Subject: [PATCH] add kibana to serilog in building-blocks --- .../docker-compose/infrastructure.yaml | 35 ++++++++++++++++++- src/BuildingBlocks/Logging/Extensions.cs | 15 ++++++-- src/BuildingBlocks/Logging/LoggerOptions.cs | 1 + .../Booking/src/Booking.Api/appsettings.json | 3 +- .../Flight/src/Flight.Api/appsettings.json | 3 +- .../src/Identity.Api/appsettings.json | 3 +- .../src/Passenger.Api/appsettings.json | 3 +- 7 files changed, 56 insertions(+), 7 deletions(-) diff --git a/deployments/docker-compose/infrastructure.yaml b/deployments/docker-compose/infrastructure.yaml index d8f0e93..70112f7 100644 --- a/deployments/docker-compose/infrastructure.yaml +++ b/deployments/docker-compose/infrastructure.yaml @@ -89,6 +89,38 @@ services: - 27017:27017 volumes: - mongo:/data/db + + + ####################################################### + # Elastic Search - Kibana + ####################################################### + elasticsearch: + container_name: elasticsearch + image: docker.elastic.co/elasticsearch/elasticsearch:7.9.2 + ports: + - 9200:9200 + volumes: + - elasticsearch-data:/usr/share/elasticsearch/data + environment: + - xpack.monitoring.enabled=true + - xpack.watcher.enabled=false + - "ES_JAVA_OPTS=-Xms512m -Xmx512m" + - discovery.type=single-node + networks: + - booking + + kibana: + container_name: kibana + image: docker.elastic.co/kibana/kibana:7.9.2 + ports: + - 5601:5601 + depends_on: + - elasticsearch + environment: + - ELASTICSEARCH_URL=http://localhost:9200 + networks: + - booking + networks: @@ -99,4 +131,5 @@ volumes: db-data: external: false mongo: - driver: local \ No newline at end of file + driver: local + elasticsearch-data: \ No newline at end of file diff --git a/src/BuildingBlocks/Logging/Extensions.cs b/src/BuildingBlocks/Logging/Extensions.cs index 6fc02ff..806be39 100644 --- a/src/BuildingBlocks/Logging/Extensions.cs +++ b/src/BuildingBlocks/Logging/Extensions.cs @@ -1,4 +1,6 @@ -using Microsoft.AspNetCore.Builder; +using System.Reflection; +using BuildingBlocks.Web; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; @@ -6,6 +8,7 @@ using Serilog; using Serilog.Enrichers.Span; using Serilog.Events; using Serilog.Exceptions; +using Serilog.Sinks.Elasticsearch; using Serilog.Sinks.SpectreConsole; namespace BuildingBlocks.Logging; @@ -20,13 +23,21 @@ public static class Extensions builder.Host.UseSerilog((context, loggerConfiguration) => { + var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); var loggOptions = 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.Console() + loggerConfiguration + .WriteTo.Console() + .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(loggOptions.ElasticUri)) + { + 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() diff --git a/src/BuildingBlocks/Logging/LoggerOptions.cs b/src/BuildingBlocks/Logging/LoggerOptions.cs index 7bc99fa..013018e 100644 --- a/src/BuildingBlocks/Logging/LoggerOptions.cs +++ b/src/BuildingBlocks/Logging/LoggerOptions.cs @@ -4,4 +4,5 @@ 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 47f9d07..7d9ab6e 100644 --- a/src/Services/Booking/src/Booking.Api/appsettings.json +++ b/src/Services/Booking/src/Booking.Api/appsettings.json @@ -4,7 +4,8 @@ }, "LogOptions": { "Level": "Information", - "LogTemplate": "{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}" + "LogTemplate": "{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}", + "ElasticUri": "http://localhost:9200" }, "ConnectionStrings": { "DefaultConnection": "Server=.\\sqlexpress;Database=BookingDB;Trusted_Connection=True;MultipleActiveResultSets=true" diff --git a/src/Services/Flight/src/Flight.Api/appsettings.json b/src/Services/Flight/src/Flight.Api/appsettings.json index 9dc79d2..879971c 100644 --- a/src/Services/Flight/src/Flight.Api/appsettings.json +++ b/src/Services/Flight/src/Flight.Api/appsettings.json @@ -4,7 +4,8 @@ }, "LogOptions": { "Level": "Information", - "LogTemplate": "{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}" + "LogTemplate": "{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}", + "ElasticUri": "http://localhost:9200" }, "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 bc7d45d..126be44 100644 --- a/src/Services/Identity/src/Identity.Api/appsettings.json +++ b/src/Services/Identity/src/Identity.Api/appsettings.json @@ -13,7 +13,8 @@ }, "LogOptions": { "Level": "Information", - "LogTemplate": "{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}" + "LogTemplate": "{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}", + "ElasticUri": "http://localhost:9200" }, "AllowedHosts": "*" } diff --git a/src/Services/Passenger/src/Passenger.Api/appsettings.json b/src/Services/Passenger/src/Passenger.Api/appsettings.json index bfb3e63..f178d51 100644 --- a/src/Services/Passenger/src/Passenger.Api/appsettings.json +++ b/src/Services/Passenger/src/Passenger.Api/appsettings.json @@ -17,7 +17,8 @@ }, "LogOptions": { "Level": "Information", - "LogTemplate": "{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}" + "LogTemplate": "{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}", + "ElasticUri": "http://localhost:9200" }, "AllowedHosts": "*" }