refactor custom serilog extensions

This commit is contained in:
meysamhadeli 2022-05-17 23:43:23 +04:30
parent d03fd7419e
commit c20bc382ee
8 changed files with 41 additions and 48 deletions

View File

@ -5,12 +5,6 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3"/>
<PackageReference Include="Yarp.ReverseProxy" Version="1.0.0"/>
<PackageReference Include="Microsoft.Identity.Web" Version="1.22.3"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\BuildingBlocks\BuildingBlocks.csproj"/>
</ItemGroup>

View File

@ -61,6 +61,7 @@
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
<PackageReference Include="Serilog.Enrichers.Span" Version="2.2.0" />
<PackageReference Include="Serilog.Exceptions" Version="8.1.0" />
<PackageReference Include="Serilog.Formatting.Elasticsearch" Version="8.4.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
<PackageReference Include="Serilog.Sinks.Elasticsearch" Version="8.4.1" />
@ -91,7 +92,9 @@
<PackageReference Include="Duende.IdentityServer.EntityFramework.Storage" Version="6.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.0" />
<PackageReference Include="Yarp.ReverseProxy" Version="1.0.0" />
<PackageReference Include="Microsoft.Identity.Web" Version="1.22.3" />
<PackageReference Include="Jaeger" Version="0.3.7" />
<PackageReference Include="OpenTracing" Version="0.12.1" />
<PackageReference Include="prometheus-net" Version="6.0.0" />

View File

@ -1,9 +1,11 @@
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.SpectreConsole;
namespace BuildingBlocks.Logging;
@ -12,20 +14,27 @@ public static class Extensions
{
public static WebApplicationBuilder AddCustomSerilog(this WebApplicationBuilder builder)
{
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateBootstrapLogger();
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateBootstrapLogger();
builder.Host.UseSerilog((context, loggerConfiguration) =>
{
var loggOptions = context.Configuration.GetSection(nameof(LogOptions)).Get<LogOptions>();
builder.Host.UseSerilog((ctx, lc) => lc
.WriteTo.Console()
.WriteTo.SpectreConsole("{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}",
LogEventLevel.Error)
var logLevel = Enum.TryParse<LogEventLevel>(loggOptions.Level, true, out var level)
? level
: LogEventLevel.Information;
loggerConfiguration.WriteTo.Console()
.WriteTo.SpectreConsole(loggOptions.LogTemplate, logLevel)
.MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Error)
.Enrich.WithSpan()
.Enrich.WithExceptionDetails()
.Enrich.FromLogContext()
.ReadFrom.Configuration(ctx.Configuration));
.ReadFrom.Configuration(context.Configuration);
});
return builder;
return builder;
}
}

View File

@ -0,0 +1,7 @@
namespace BuildingBlocks.Logging;
public class LogOptions
{
public string Level { get; set; }
public string LogTemplate { get; set; }
}

View File

@ -2,14 +2,9 @@
"AppOptions": {
"Name": "Booking-Service"
},
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
"LogOptions": {
"Level": "Information",
"LogTemplate": "{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}"
},
"ConnectionStrings": {
"DefaultConnection": "Server=.\\sqlexpress;Database=BookingDB;Trusted_Connection=True;MultipleActiveResultSets=true"

View File

@ -2,14 +2,9 @@
"AppOptions": {
"Name": "Flight-Service"
},
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
"LogOptions": {
"Level": "Information",
"LogTemplate": "{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}"
},
"ConnectionStrings": {
"DefaultConnection": "Server=.\\sqlexpress;Database=FlightDB;Trusted_Connection=True;MultipleActiveResultSets=true"

View File

@ -11,14 +11,9 @@
"UserName": "guest",
"Password": "guest"
},
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
"LogOptions": {
"Level": "Information",
"LogTemplate": "{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}"
},
"AllowedHosts": "*"
}

View File

@ -15,14 +15,9 @@
"UserName": "guest",
"Password": "guest"
},
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
"LogOptions": {
"Level": "Information",
"LogTemplate": "{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}"
},
"AllowedHosts": "*"
}