feat: configure Serilog for file logging with environment-specific settings
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user