Enhance logging and update solution structure
Replaced `DigitalData.MessagingService.Client.DependencyInjection` with `DigitalData.MessagingService.Client` in the solution file, updating project references and build configurations. Improved logging by integrating Serilog's SQLite sink and `Serilog.UI` for web-based log visualization. Added dynamic log directory resolution and SQLite-based log storage in `Program.cs`. Enhanced `DigitalData.MessagingService.API.csproj` with metadata properties for better documentation and packaging. Added new NuGet dependencies for logging and removed unused `<Folder>` entries. Updated `appsettings.json` to include `Application:LogDirectory` and `Swagger:Enabled` settings for configurable logging and Swagger availability.
This commit is contained in:
@@ -3,15 +3,40 @@ using DigitalData.MessagingService.Application;
|
||||
using DigitalData.MessagingService.Application.Common.Dtos;
|
||||
using DigitalData.MessagingService.Infrastructure;
|
||||
using Serilog;
|
||||
using Serilog.Ui.Core.Extensions;
|
||||
using Serilog.Ui.SqliteDataProvider.Extensions;
|
||||
using Serilog.Ui.Web.Extensions;
|
||||
|
||||
// Configure Serilog early
|
||||
// Build temporary configuration to read log directory early
|
||||
var tempConfig = new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: false)
|
||||
.AddJsonFile("appsettings.Secrets.json", optional: true, reloadOnChange: false)
|
||||
.Build();
|
||||
|
||||
var logDirectoryRaw = tempConfig.GetValue<string>("Application:LogDirectory") ?? "logs";
|
||||
// Always resolve to an absolute path so sink and UI provider point to the same file
|
||||
var logDirectory = Path.IsPathRooted(logDirectoryRaw)
|
||||
? logDirectoryRaw
|
||||
: Path.Combine(AppContext.BaseDirectory, logDirectoryRaw);
|
||||
Directory.CreateDirectory(logDirectory);
|
||||
var sqliteDbPath = Path.Combine(logDirectory, "logs.db");
|
||||
|
||||
// Configure Serilog early (bootstrap + full pipeline)
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.WriteTo.Console()
|
||||
.MinimumLevel.Information()
|
||||
.MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning)
|
||||
.MinimumLevel.Override("Microsoft.AspNetCore", Serilog.Events.LogEventLevel.Warning)
|
||||
.MinimumLevel.Override("Microsoft.EntityFrameworkCore", Serilog.Events.LogEventLevel.Warning)
|
||||
.MinimumLevel.Override("System", Serilog.Events.LogEventLevel.Warning)
|
||||
.Enrich.FromLogContext()
|
||||
.WriteTo.Console(outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}")
|
||||
.WriteTo.File(
|
||||
path: "logs/emailprofiler-.log",
|
||||
path: Path.Combine(logDirectory, "emailprofiler-.log"),
|
||||
rollingInterval: RollingInterval.Day,
|
||||
retainedFileCountLimit: 30,
|
||||
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}")
|
||||
.WriteTo.SQLite(sqliteDbPath, storeTimestampInUtc: true)
|
||||
.CreateBootstrapLogger();
|
||||
|
||||
try
|
||||
@@ -25,12 +50,13 @@ try
|
||||
.ReadFrom.Configuration(context.Configuration)
|
||||
.ReadFrom.Services(services)
|
||||
.Enrich.FromLogContext()
|
||||
.WriteTo.Console()
|
||||
.WriteTo.Console(outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}")
|
||||
.WriteTo.File(
|
||||
path: "logs/emailprofiler-.log",
|
||||
path: Path.Combine(logDirectory, "emailprofiler-.log"),
|
||||
rollingInterval: RollingInterval.Day,
|
||||
retainedFileCountLimit: 30,
|
||||
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}"));
|
||||
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}")
|
||||
.WriteTo.SQLite(sqliteDbPath, storeTimestampInUtc: true));
|
||||
|
||||
// Add appsettings.Secrets.json for sensitive configuration (not committed to git)
|
||||
builder.Configuration.AddJsonFile("appsettings.Secrets.json", optional: true, reloadOnChange: true);
|
||||
@@ -50,6 +76,16 @@ try
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
// Register Serilog.UI with SQLite provider for web log viewer
|
||||
builder.Services.AddSerilogUi(options =>
|
||||
{
|
||||
options.UseSqliteServer(dbOpt =>
|
||||
{
|
||||
dbOpt.WithConnectionString($"Data Source={sqliteDbPath}");
|
||||
dbOpt.WithTable("Logs");
|
||||
});
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Add global exception handling middleware
|
||||
@@ -58,8 +94,11 @@ try
|
||||
// Add Serilog request logging
|
||||
app.UseSerilogRequestLogging();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
// Configure Swagger — enabled in Development always, and in other environments based on appsettings
|
||||
var swaggerEnabled = app.Environment.IsDevelopment()
|
||||
|| app.Configuration.GetValue<bool>("Swagger:Enabled");
|
||||
|
||||
if (swaggerEnabled)
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
@@ -67,6 +106,9 @@ try
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
// Serve Serilog.UI log viewer at /serilog-ui
|
||||
app.UseSerilogUi();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
Reference in New Issue
Block a user