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

View File

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

View File

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

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": { "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"

View File

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

View File

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

View File

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