feat(logging): Configure Serilog SQLite sink + UI
appsettings.json: - Add Application:LogDirectory configuration - Add Serilog WriteTo.SQLite sink (logs.db in LogDirectory) - Logs table with UTC timestamps Program.cs: - Add Serilog.UI usings - Configure Serilog.UI with SQLite provider - Mount UI at /serilog-ui endpoint Web-based log viewer accessible at: https://localhost:7186/serilog-ui
This commit is contained in:
@@ -1,4 +1,7 @@
|
|||||||
using Serilog;
|
using Serilog;
|
||||||
|
using Serilog.Ui.Core.Extensions;
|
||||||
|
using Serilog.Ui.SqliteDataProvider.Extensions;
|
||||||
|
using Serilog.Ui.Web.Extensions;
|
||||||
using DocumentOperator.Infrastructure.Configuration;
|
using DocumentOperator.Infrastructure.Configuration;
|
||||||
using DocumentOperator.Application;
|
using DocumentOperator.Application;
|
||||||
using DocumentOperator.Infrastructure;
|
using DocumentOperator.Infrastructure;
|
||||||
@@ -45,7 +48,23 @@ try
|
|||||||
builder.Services.AddSwaggerDocumentation();
|
builder.Services.AddSwaggerDocumentation();
|
||||||
|
|
||||||
// ========================================
|
// ========================================
|
||||||
// 4. Build App
|
// 4. Serilog.UI Configuration
|
||||||
|
// ========================================
|
||||||
|
var logDirectory = builder.Configuration.GetValue<string>("Application:LogDirectory")
|
||||||
|
?? throw new InvalidOperationException("Application:LogDirectory not found in configuration.");
|
||||||
|
var sqliteDbPath = Path.Combine(logDirectory, "logs.db");
|
||||||
|
|
||||||
|
builder.Services.AddSerilogUi(logUIOpt =>
|
||||||
|
{
|
||||||
|
logUIOpt.UseSqliteServer(dbOpt =>
|
||||||
|
{
|
||||||
|
dbOpt.WithConnectionString($"Data Source={sqliteDbPath}");
|
||||||
|
dbOpt.WithTable("Logs");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ========================================
|
||||||
|
// 5. Build App
|
||||||
// ========================================
|
// ========================================
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
@@ -67,7 +86,12 @@ try
|
|||||||
app.UseHttpsRedirection();
|
app.UseHttpsRedirection();
|
||||||
|
|
||||||
// ========================================
|
// ========================================
|
||||||
// 6. Endpoints (Controller-based API)
|
// 6. Serilog.UI Dashboard
|
||||||
|
// ========================================
|
||||||
|
app.UseSerilogUi(); // Accessible at /serilog-ui
|
||||||
|
|
||||||
|
// ========================================
|
||||||
|
// 7. Endpoints (Controller-based API)
|
||||||
// ========================================
|
// ========================================
|
||||||
app.MapControllers(); // Maps all [ApiController] controllers
|
app.MapControllers(); // Maps all [ApiController] controllers
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,10 @@
|
|||||||
},
|
},
|
||||||
"AllowedHosts": "*",
|
"AllowedHosts": "*",
|
||||||
|
|
||||||
|
"Application": {
|
||||||
|
"LogDirectory": "E:\\LogFiles\\Digital Data\\DocumentOperator.API"
|
||||||
|
},
|
||||||
|
|
||||||
"Serilog": {
|
"Serilog": {
|
||||||
"MinimumLevel": {
|
"MinimumLevel": {
|
||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
@@ -30,6 +34,14 @@
|
|||||||
"rollingInterval": "Day",
|
"rollingInterval": "Day",
|
||||||
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}"
|
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Name": "SQLite",
|
||||||
|
"Args": {
|
||||||
|
"sqliteDbPath": "E:\\LogFiles\\Digital Data\\DocumentOperator.API\\logs.db",
|
||||||
|
"tableName": "Logs",
|
||||||
|
"storeTimestampInUtc": true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ]
|
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ]
|
||||||
|
|||||||
Reference in New Issue
Block a user