add serilog

This commit is contained in:
tekh 2025-10-31 10:23:11 +01:00
parent bd6d57e1e8
commit 7c5a505ad1
3 changed files with 89 additions and 27 deletions

View File

@ -10,6 +10,10 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.10" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.10" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" /> <PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="9.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -2,36 +2,62 @@ using EnvelopeGenerator.Application;
using EnvelopeGenerator.Finalizer; using EnvelopeGenerator.Finalizer;
using EnvelopeGenerator.Infrastructure; using EnvelopeGenerator.Infrastructure;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Serilog;
var builder = Host.CreateApplicationBuilder(args); // Load Serilog from appsettings.json
var config = builder.Configuration; Log.Logger = new LoggerConfiguration()
builder.Services.AddHostedService<Worker>(); .ReadFrom.Configuration(new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build())
.CreateLogger();
#region Add DB Context, EG Inf. and Services try
var cnnStrName = "Default"; {
var connStr = config.GetConnectionString(cnnStrName) var builder = Host.CreateApplicationBuilder(args);
?? throw new InvalidOperationException($"Connection string '{cnnStrName}' is missing in the application configuration.");
#pragma warning disable CS0618 // Type or member is obsolete // add serilog
builder.Services.AddEnvelopeGeneratorInfrastructureServices( builder.Logging.ClearProviders();
opt => builder.Logging.AddSerilog();
{
opt.AddDbTriggerParams(config); var config = builder.Configuration;
opt.AddDbContext((provider, options) => builder.Services.AddHostedService<Worker>();
#region Add DB Context, EG Inf. and Services
var cnnStrName = "Default";
var connStr = config.GetConnectionString(cnnStrName)
?? throw new InvalidOperationException($"Connection string '{cnnStrName}' is missing in the application configuration.");
#pragma warning disable CS0618 // Type or member is obsolete
builder.Services.AddEnvelopeGeneratorInfrastructureServices(
opt =>
{ {
var logger = provider.GetRequiredService<ILogger<EGDbContext>>(); opt.AddDbTriggerParams(config);
options.UseSqlServer(connStr) opt.AddDbContext((provider, options) =>
.LogTo(log => logger.LogInformation("{log}", log), Microsoft.Extensions.Logging.LogLevel.Trace) {
.EnableSensitiveDataLogging() var logger = provider.GetRequiredService<ILogger<EGDbContext>>();
.EnableDetailedErrors(); options.UseSqlServer(connStr)
.LogTo(log => logger.LogInformation("{log}", log), Microsoft.Extensions.Logging.LogLevel.Trace)
.EnableSensitiveDataLogging()
.EnableDetailedErrors();
});
}); });
}); #pragma warning restore CS0618 // Type or member is obsolete
#pragma warning restore CS0618 // Type or member is obsolete
#pragma warning disable CS0618 // Type or member is obsolete #pragma warning disable CS0618 // Type or member is obsolete
builder.Services.AddEnvelopeGeneratorServices(config); builder.Services.AddEnvelopeGeneratorServices(config);
#pragma warning restore CS0618 // Type or member is obsolete #pragma warning restore CS0618 // Type or member is obsolete
#endregion Add DB Context, EG Inf. and Services #endregion Add DB Context, EG Inf. and Services
var host = builder.Build(); var host = builder.Build();
host.Run(); host.Run();
Log.Information("The worker was stopped.");
}
catch(Exception ex)
{
Log.Fatal(ex, "Worker could not be started!");
}
finally
{
Log.CloseAndFlush();
}

View File

@ -4,5 +4,37 @@
"Default": "Information", "Default": "Information",
"Microsoft.Hosting.Lifetime": "Information" "Microsoft.Hosting.Lifetime": "Information"
} }
},
"ConnectionStrings": {
"Default": "Server=SDD-VMP04-SQL17\\DD_DEVELOP01;Database=DD_ECM;User Id=sa;Password=dd;Encrypt=false;TrustServerCertificate=True;",
"DbMigrationTest": "Server=SDD-VMP04-SQL17\\DD_DEVELOP01;Database=DD_ECM_DATA_MIGR_TEST;User Id=sa;Password=dd;Encrypt=false;TrustServerCertificate=True;"
},
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "Console",
"Args": {
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"
}
},
{
"Name": "File",
"Args": {
"path": "Logs/log-.txt",
"rollingInterval": "Day",
"retainedFileCountLimit": 7,
"outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ]
} }
} }