add kibana to serilog in building-blocks

This commit is contained in:
meysamhadeli 2022-05-25 01:39:40 +04:30
parent 7b1e1f0e43
commit ccb7ffa035
7 changed files with 56 additions and 7 deletions

View File

@ -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
driver: local
elasticsearch-data:

View File

@ -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<LogOptions>();
var appOptions = context.Configuration.GetSection(nameof(AppOptions)).Get<AppOptions>();
var logLevel = Enum.TryParse<LogEventLevel>(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()

View File

@ -4,4 +4,5 @@ public class LogOptions
{
public string Level { get; set; }
public string LogTemplate { get; set; }
public string ElasticUri { get; set; }
}

View File

@ -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"

View File

@ -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"

View File

@ -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": "*"
}

View File

@ -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": "*"
}