From 209aa5912dad00d45efb571db645ce6413ce60ca Mon Sep 17 00:00:00 2001 From: mahan Date: Mon, 5 Jan 2026 10:58:30 +0330 Subject: [PATCH] feat: configure Serilog for file logging with environment-specific settings --- ServiceHost/Program.cs | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/ServiceHost/Program.cs b/ServiceHost/Program.cs index 921656cd..73ff3d88 100644 --- a/ServiceHost/Program.cs +++ b/ServiceHost/Program.cs @@ -63,16 +63,10 @@ if (!Directory.Exists(logDirectory)) Directory.CreateDirectory(logDirectory); } +// فقط برای فایل از Serilog استفاده می‌شود +// تنظیمات MinimumLevel از appsettings.json خوانده می‌شود Log.Logger = new LoggerConfiguration() - //NO EF Core log - .MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning) - - //NO DbCommand log - .MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", LogEventLevel.Warning) - - //NO Microsoft Public log - .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) - //.MinimumLevel.Information() + .Enrich.FromLogContext() .WriteTo.File( path: Path.Combine(logDirectory, "gozareshgir_log.txt"), rollingInterval: RollingInterval.Day, @@ -379,7 +373,30 @@ builder.Services.AddParbad().ConfigureGateways(gateways => }); -builder.Host.UseSerilog(); +// فقط Serilog برای File استفاده می‌شه، کنسول از لاگر پیش‌فرض ASP.NET استفاده می‌کنه +builder.Host.UseSerilog((context, services, configuration) => +{ + var logConfig = configuration + .ReadFrom.Configuration(context.Configuration) + .ReadFrom.Services(services) + .Enrich.FromLogContext(); + + // در محیط Development، EF Core Commands را هم لاگ می‌کنیم + if (context.HostingEnvironment.IsDevelopment()) + { + logConfig + .MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", LogEventLevel.Information); + } + + logConfig.WriteTo.File( + path: Path.Combine(logDirectory, "gozareshgir_log.txt"), + rollingInterval: RollingInterval.Day, + retainedFileCountLimit: 30, + shared: true, + outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level}] {Message}{NewLine}{Exception}" + ); +}, writeToProviders: true); // این باعث میشه کنسول پیش‌فرض هم کار کنه + Log.Information("SERILOG STARTED SUCCESSFULLY"); var app = builder.Build();