Configure seq logs

This commit is contained in:
Michał Zieliński
2025-06-02 17:20:49 +02:00
parent 099d72618f
commit 13ac9e68ba
3 changed files with 85 additions and 3 deletions

View File

@@ -10,9 +10,22 @@ using System.Text;
using DiunaBI.Database.Context;
using DiunaBI.Core.Services;
using Google.Apis.Sheets.v4;
using Serilog;
var builder = WebApplication.CreateBuilder(args);
// ✅ DODAJ SERILOG CONFIGURATION
builder.Host.UseSerilog((context, configuration) =>
{
configuration
.ReadFrom.Configuration(context.Configuration)
.Enrich.FromLogContext()
.Enrich.WithProperty("Application", "DiunaBI")
.Enrich.WithProperty("Version", Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown")
.Enrich.WithEnvironmentName()
.Enrich.WithMachineName();
});
var connectionString = builder.Configuration.GetConnectionString("SQLDatabase");
builder.Services.AddDbContext<AppDbContext>(x =>
{
@@ -91,12 +104,37 @@ builder.Services.AddSingleton<PluginManager>();
var app = builder.Build();
var pluginManager = app.Services.GetRequiredService<PluginManager>();
// ✅ DODAJ SERILOG REQUEST LOGGING
app.UseSerilogRequestLogging(options =>
{
options.MessageTemplate = "HTTP {RequestMethod} {RequestPath} responded {StatusCode} in {Elapsed:0.0000} ms";
options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
{
diagnosticContext.Set("RequestHost", httpContext.Request.Host.Value);
diagnosticContext.Set("RequestScheme", httpContext.Request.Scheme);
// ✅ POPRAW NULLABLE WARNING
var userAgent = httpContext.Request.Headers.UserAgent.FirstOrDefault();
if (!string.IsNullOrEmpty(userAgent))
{
diagnosticContext.Set("UserAgent", userAgent);
}
// Dodaj więcej użytecznych właściwości
diagnosticContext.Set("RemoteIP", httpContext.Connection.RemoteIpAddress?.ToString() ?? "unknown");
diagnosticContext.Set("RequestContentType", httpContext.Request.ContentType ?? "none");
};
});
// Załaduj pluginy - z logowaniem
var pluginManager = app.Services.GetRequiredService<PluginManager>();
var executablePath = Assembly.GetExecutingAssembly().Location;
var executableDir = Path.GetDirectoryName(executablePath)!;
var pluginsPath = Path.Combine(executableDir, "Plugins");
Log.Information("Starting DiunaBI application");
Log.Information("Loading plugins from: {PluginsPath}", pluginsPath);
pluginManager.LoadPluginsFromDirectory(pluginsPath);
app.Use(async (context, next) =>
@@ -122,3 +160,6 @@ app.UseAuthorization();
app.MapControllers();
app.Run();
// ✅ DODAJ CLEANUP
Log.CloseAndFlush();