mirror of
https://github.com/meysamhadeli/booking-microservices.git
synced 2026-05-07 06:32:08 +08:00
refactor custom serilog extensions
This commit is contained in:
parent
d03fd7419e
commit
c20bc382ee
@ -5,12 +5,6 @@
|
|||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
</PropertyGroup>
|
</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>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\BuildingBlocks\BuildingBlocks.csproj"/>
|
<ProjectReference Include="..\..\BuildingBlocks\BuildingBlocks.csproj"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@ -61,6 +61,7 @@
|
|||||||
<PackageReference Include="Serilog" Version="2.10.0" />
|
<PackageReference Include="Serilog" Version="2.10.0" />
|
||||||
<PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
|
<PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
|
||||||
<PackageReference Include="Serilog.Enrichers.Span" Version="2.2.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.Formatting.Elasticsearch" Version="8.4.1" />
|
||||||
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
|
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
|
||||||
<PackageReference Include="Serilog.Sinks.Elasticsearch" Version="8.4.1" />
|
<PackageReference Include="Serilog.Sinks.Elasticsearch" Version="8.4.1" />
|
||||||
@ -91,6 +92,8 @@
|
|||||||
<PackageReference Include="Duende.IdentityServer.EntityFramework.Storage" Version="6.0.3" />
|
<PackageReference Include="Duende.IdentityServer.EntityFramework.Storage" Version="6.0.3" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.1" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.1" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.0" />
|
<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="Jaeger" Version="0.3.7" />
|
||||||
<PackageReference Include="OpenTracing" Version="0.12.1" />
|
<PackageReference Include="OpenTracing" Version="0.12.1" />
|
||||||
|
|||||||
@ -1,9 +1,11 @@
|
|||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
using Serilog.Enrichers.Span;
|
using Serilog.Enrichers.Span;
|
||||||
using Serilog.Events;
|
using Serilog.Events;
|
||||||
|
using Serilog.Exceptions;
|
||||||
using Serilog.Sinks.SpectreConsole;
|
using Serilog.Sinks.SpectreConsole;
|
||||||
|
|
||||||
namespace BuildingBlocks.Logging;
|
namespace BuildingBlocks.Logging;
|
||||||
@ -12,19 +14,26 @@ public static class Extensions
|
|||||||
{
|
{
|
||||||
public static WebApplicationBuilder AddCustomSerilog(this WebApplicationBuilder builder)
|
public static WebApplicationBuilder AddCustomSerilog(this WebApplicationBuilder builder)
|
||||||
{
|
{
|
||||||
|
|
||||||
Log.Logger = new LoggerConfiguration()
|
Log.Logger = new LoggerConfiguration()
|
||||||
.WriteTo.Console()
|
.WriteTo.Console()
|
||||||
.CreateBootstrapLogger();
|
.CreateBootstrapLogger();
|
||||||
|
|
||||||
builder.Host.UseSerilog((ctx, lc) => lc
|
builder.Host.UseSerilog((context, loggerConfiguration) =>
|
||||||
.WriteTo.Console()
|
{
|
||||||
.WriteTo.SpectreConsole("{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}",
|
var loggOptions = context.Configuration.GetSection(nameof(LogOptions)).Get<LogOptions>();
|
||||||
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)
|
.MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Error)
|
||||||
.Enrich.WithSpan()
|
.Enrich.WithSpan()
|
||||||
|
.Enrich.WithExceptionDetails()
|
||||||
.Enrich.FromLogContext()
|
.Enrich.FromLogContext()
|
||||||
.ReadFrom.Configuration(ctx.Configuration));
|
.ReadFrom.Configuration(context.Configuration);
|
||||||
|
});
|
||||||
|
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
|
|||||||
7
src/BuildingBlocks/Logging/LoggerOptions.cs
Normal file
7
src/BuildingBlocks/Logging/LoggerOptions.cs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
namespace BuildingBlocks.Logging;
|
||||||
|
|
||||||
|
public class LogOptions
|
||||||
|
{
|
||||||
|
public string Level { get; set; }
|
||||||
|
public string LogTemplate { get; set; }
|
||||||
|
}
|
||||||
@ -2,14 +2,9 @@
|
|||||||
"AppOptions": {
|
"AppOptions": {
|
||||||
"Name": "Booking-Service"
|
"Name": "Booking-Service"
|
||||||
},
|
},
|
||||||
"Serilog": {
|
"LogOptions": {
|
||||||
"MinimumLevel": {
|
"Level": "Information",
|
||||||
"Default": "Information",
|
"LogTemplate": "{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}"
|
||||||
"Override": {
|
|
||||||
"Microsoft": "Warning",
|
|
||||||
"Microsoft.Hosting.Lifetime": "Information"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"DefaultConnection": "Server=.\\sqlexpress;Database=BookingDB;Trusted_Connection=True;MultipleActiveResultSets=true"
|
"DefaultConnection": "Server=.\\sqlexpress;Database=BookingDB;Trusted_Connection=True;MultipleActiveResultSets=true"
|
||||||
|
|||||||
@ -2,14 +2,9 @@
|
|||||||
"AppOptions": {
|
"AppOptions": {
|
||||||
"Name": "Flight-Service"
|
"Name": "Flight-Service"
|
||||||
},
|
},
|
||||||
"Serilog": {
|
"LogOptions": {
|
||||||
"MinimumLevel": {
|
"Level": "Information",
|
||||||
"Default": "Information",
|
"LogTemplate": "{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}"
|
||||||
"Override": {
|
|
||||||
"Microsoft": "Warning",
|
|
||||||
"Microsoft.Hosting.Lifetime": "Information"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"DefaultConnection": "Server=.\\sqlexpress;Database=FlightDB;Trusted_Connection=True;MultipleActiveResultSets=true"
|
"DefaultConnection": "Server=.\\sqlexpress;Database=FlightDB;Trusted_Connection=True;MultipleActiveResultSets=true"
|
||||||
|
|||||||
@ -11,14 +11,9 @@
|
|||||||
"UserName": "guest",
|
"UserName": "guest",
|
||||||
"Password": "guest"
|
"Password": "guest"
|
||||||
},
|
},
|
||||||
"Serilog": {
|
"LogOptions": {
|
||||||
"MinimumLevel": {
|
"Level": "Information",
|
||||||
"Default": "Information",
|
"LogTemplate": "{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}"
|
||||||
"Override": {
|
|
||||||
"Microsoft": "Warning",
|
|
||||||
"Microsoft.Hosting.Lifetime": "Information"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*"
|
"AllowedHosts": "*"
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,14 +15,9 @@
|
|||||||
"UserName": "guest",
|
"UserName": "guest",
|
||||||
"Password": "guest"
|
"Password": "guest"
|
||||||
},
|
},
|
||||||
"Serilog": {
|
"LogOptions": {
|
||||||
"MinimumLevel": {
|
"Level": "Information",
|
||||||
"Default": "Information",
|
"LogTemplate": "{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}"
|
||||||
"Override": {
|
|
||||||
"Microsoft": "Warning",
|
|
||||||
"Microsoft.Hosting.Lifetime": "Information"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*"
|
"AllowedHosts": "*"
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user