From fb62523a23a54dc879d79cda07bc7ab293e50081 Mon Sep 17 00:00:00 2001 From: mahan Date: Wed, 31 Dec 2025 19:06:56 +0330 Subject: [PATCH] add: integrate Serilog for enhanced logging and monitoring --- .../PaymentGateway/SepehrPaymentGateway.cs | 12 +++++-- ServiceHost/Program.cs | 34 ++++++++++++++++++- ServiceHost/ServiceHost.csproj | 5 +++ 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/0_Framework/Application/PaymentGateway/SepehrPaymentGateway.cs b/0_Framework/Application/PaymentGateway/SepehrPaymentGateway.cs index 792df36b..874df523 100644 --- a/0_Framework/Application/PaymentGateway/SepehrPaymentGateway.cs +++ b/0_Framework/Application/PaymentGateway/SepehrPaymentGateway.cs @@ -4,6 +4,7 @@ using System.Net.Http; using System.Net.Http.Json; using System.Threading; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; using Newtonsoft.Json; namespace _0_Framework.Application.PaymentGateway; @@ -12,9 +13,11 @@ public class SepehrPaymentGateway:IPaymentGateway { private readonly HttpClient _httpClient; private const long TerminalId = 99213700; + private readonly ILogger _logger; - public SepehrPaymentGateway(IHttpClientFactory httpClient) + public SepehrPaymentGateway(IHttpClientFactory httpClient, ILogger logger) { + _logger = logger; _httpClient = httpClient.CreateClient(); _httpClient.BaseAddress = new Uri("https://sepehr.shaparak.ir/Rest/V1/PeymentApi/"); } @@ -35,6 +38,7 @@ public class SepehrPaymentGateway:IPaymentGateway // خواندن محتوای پاسخ var content = await res.Content.ReadAsStringAsync(cancellationToken); + // تبدیل پاسخ JSON به آبجکت دات‌نت var json = System.Text.Json.JsonDocument.Parse(content); @@ -46,7 +50,7 @@ public class SepehrPaymentGateway:IPaymentGateway { Status = status, IsSuccess = status == "0", - Token = accessToken + Token = accessToken, }; } @@ -63,7 +67,9 @@ public class SepehrPaymentGateway:IPaymentGateway // خواندن محتوای پاسخ var content = await res.Content.ReadAsStringAsync(cancellationToken); - + + _logger.LogInformation(content); + // تبدیل پاسخ JSON به آبجکت دات‌نت var json = System.Text.Json.JsonDocument.Parse(content); diff --git a/ServiceHost/Program.cs b/ServiceHost/Program.cs index 53ed6274..921656cd 100644 --- a/ServiceHost/Program.cs +++ b/ServiceHost/Program.cs @@ -32,6 +32,8 @@ using GozareshgirProgramManager.Application.Modules.Users.Commands.CreateUser; using GozareshgirProgramManager.Infrastructure; using GozareshgirProgramManager.Infrastructure.Persistence.Seed; using Microsoft.OpenApi; +using Serilog; +using Serilog.Events; using ServiceHost.Hubs.ProgramManager; using ServiceHost.Notifications.ProgramManager; using ServiceHost.Conventions; @@ -53,7 +55,36 @@ builder.Services.AddHttpClient("holidayApi", c => c.BaseAddress = new System.Uri var connectionString = builder.Configuration.GetConnectionString("MesbahDb"); var connectionStringTestDb = builder.Configuration.GetConnectionString("TestDb"); +#region Serilog +var logDirectory = @"C:\Logs\Gozareshgir\"; +if (!Directory.Exists(logDirectory)) +{ + Directory.CreateDirectory(logDirectory); +} + +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() + .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}" + ).CreateLogger(); + + + +#endregion builder.Services.AddProgramManagerApplication(); builder.Services.AddProgramManagerInfrastructure(builder.Configuration); @@ -348,7 +379,8 @@ builder.Services.AddParbad().ConfigureGateways(gateways => }); - +builder.Host.UseSerilog(); +Log.Information("SERILOG STARTED SUCCESSFULLY"); var app = builder.Build(); app.UseCors("AllowSpecificOrigins"); diff --git a/ServiceHost/ServiceHost.csproj b/ServiceHost/ServiceHost.csproj index 744e0213..8eb8a743 100644 --- a/ServiceHost/ServiceHost.csproj +++ b/ServiceHost/ServiceHost.csproj @@ -94,11 +94,16 @@ + + + + +