From 0902bffd9bdc4b7ce73e4128589e93bb437bd669 Mon Sep 17 00:00:00 2001 From: zhangxiaofeng <1939543722@qq.com> Date: Thu, 11 Apr 2024 17:40:15 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B4=B9=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LogBatchingSinkConfiguration.cs | 31 ---- .../EntrustSettle.Serilog.csproj | 13 -- .../LoggerConfigurationExtensions.cs | 90 ------------ EntrustSettle.Serilog/Sink/LogBatchingSink.cs | 135 ------------------ .../Utility/SerilogRequestUtility.cs | 73 ---------- .../QuartzNet/Jobs/JobHydStatusQuartz.cs | 22 +-- 6 files changed, 11 insertions(+), 353 deletions(-) delete mode 100644 EntrustSettle.Serilog/Configuration/LogBatchingSinkConfiguration.cs delete mode 100644 EntrustSettle.Serilog/EntrustSettle.Serilog.csproj delete mode 100644 EntrustSettle.Serilog/Extensions/LoggerConfigurationExtensions.cs delete mode 100644 EntrustSettle.Serilog/Sink/LogBatchingSink.cs delete mode 100644 EntrustSettle.Serilog/Utility/SerilogRequestUtility.cs diff --git a/EntrustSettle.Serilog/Configuration/LogBatchingSinkConfiguration.cs b/EntrustSettle.Serilog/Configuration/LogBatchingSinkConfiguration.cs deleted file mode 100644 index 17917ff..0000000 --- a/EntrustSettle.Serilog/Configuration/LogBatchingSinkConfiguration.cs +++ /dev/null @@ -1,31 +0,0 @@ -using EntrustSettle.Common; -using EntrustSettle.Serilog.Sink; -using Serilog; -using Serilog.Sinks.PeriodicBatching; - -namespace EntrustSettle.Serilog.Configuration; - -public static class LogBatchingSinkConfiguration -{ - public static LoggerConfiguration WriteToLogBatching(this LoggerConfiguration loggerConfiguration) - { - if (!AppSettings.app("AppSettings", "LogToDb").ObjToBool()) - { - return loggerConfiguration; - } - - var exampleSink = new LogBatchingSink(); - - var batchingOptions = new PeriodicBatchingSinkOptions - { - BatchSizeLimit = 500, - Period = TimeSpan.FromSeconds(1), - EagerlyEmitFirstEvent = true, - QueueLimit = 10000 - }; - - var batchingSink = new PeriodicBatchingSink(exampleSink, batchingOptions); - - return loggerConfiguration.WriteTo.Sink(batchingSink); - } -} \ No newline at end of file diff --git a/EntrustSettle.Serilog/EntrustSettle.Serilog.csproj b/EntrustSettle.Serilog/EntrustSettle.Serilog.csproj deleted file mode 100644 index 2e48fdc..0000000 --- a/EntrustSettle.Serilog/EntrustSettle.Serilog.csproj +++ /dev/null @@ -1,13 +0,0 @@ - - - - net8.0 - enable - NETSDK1206 - - - - - - - diff --git a/EntrustSettle.Serilog/Extensions/LoggerConfigurationExtensions.cs b/EntrustSettle.Serilog/Extensions/LoggerConfigurationExtensions.cs deleted file mode 100644 index 760c26f..0000000 --- a/EntrustSettle.Serilog/Extensions/LoggerConfigurationExtensions.cs +++ /dev/null @@ -1,90 +0,0 @@ -using EntrustSettle.Common.LogHelper; -using Serilog; -using Serilog.Events; -using Serilog.Filters; -using SqlSugar; - -namespace EntrustSettle.Serilog.Extensions; - -public static class LoggerConfigurationExtensions -{ - public static LoggerConfiguration WriteToConsole(this LoggerConfiguration loggerConfiguration) - { - //输出普通日志 - loggerConfiguration = loggerConfiguration.WriteTo.Logger(lg => lg.FilterRemoveSqlLog().WriteTo.Console()); - - //输出SQL - loggerConfiguration = loggerConfiguration.WriteTo.Logger(lg => lg.FilterSqlLog().Filter - .ByIncludingOnly(Matching.WithProperty(LogContextStatic.SqlOutToConsole, s => s)) - .WriteTo - .Console()); - - return loggerConfiguration; - } - - public static LoggerConfiguration WriteToFile(this LoggerConfiguration loggerConfiguration) - { - //输出SQL - loggerConfiguration = loggerConfiguration.WriteTo.Logger(lg => lg.FilterSqlLog() - .Filter - .ByIncludingOnly(Matching.WithProperty(LogContextStatic.SqlOutToFile, s => s)) - .WriteTo - .Async(s => s.File(LogContextStatic.Combine(DateTime.Now.ToString("yyyyMMdd"), @$"AOPSql_{DateTime.Now.DateToTimeStamp()}.log"), - rollingInterval: RollingInterval.Day, - outputTemplate: LogContextStatic.FileMessageTemplate, - retainedFileCountLimit: 31))); - //输出普通日志 - loggerConfiguration = loggerConfiguration.WriteTo.Logger(lg => lg.FilterRemoveSqlLog() - .WriteTo - .Async(s => s.File(LogContextStatic.Combine(DateTime.Now.ToString("yyyyMMdd"), @$"Log_{DateTime.Now.DateToTimeStamp()}.log"), - rollingInterval: RollingInterval.Day, - outputTemplate: LogContextStatic.FileMessageTemplate, - retainedFileCountLimit: 31))); - - return loggerConfiguration; - } - - public static LoggerConfiguration FilterSqlLog(this LoggerConfiguration lc) - { - lc = lc.Filter.ByIncludingOnly(Matching.WithProperty(LogContextStatic.LogSource, s => LogContextStatic.AopSql.Equals(s))); - return lc; - } - - public static IEnumerable FilterSqlLog(this IEnumerable batch) - { - //只记录 Insert、Update、Delete语句 - return batch.Where(s => s.WithProperty(LogContextStatic.LogSource, q => LogContextStatic.AopSql.Equals(q))) - .Where(s => s.WithProperty(LogContextStatic.SugarActionType, - q => !new[] { SugarActionType.UnKnown, SugarActionType.Query }.Contains(q))); - } - - public static LoggerConfiguration FilterRemoveSqlLog(this LoggerConfiguration lc) - { - lc = lc.Filter.ByIncludingOnly(WithProperty(LogContextStatic.LogSource, s => !LogContextStatic.AopSql.Equals(s))); - return lc; - } - - public static IEnumerable FilterRemoveOtherLog(this IEnumerable batch) - { - return batch.Where(s => WithProperty(LogContextStatic.LogSource, - q => !LogContextStatic.AopSql.Equals(q))(s)); - } - - public static Func WithProperty(string propertyName, Func predicate) - { - //如果不包含属性 也认为是true - return e => - { - if (!e.Properties.TryGetValue(propertyName, out var propertyValue)) return true; - - return propertyValue is ScalarValue { Value: T value } && predicate(value); - }; - } - - public static bool WithProperty(this LogEvent e, string key, Func predicate) - { - if (!e.Properties.TryGetValue(key, out var propertyValue)) return false; - - return propertyValue is ScalarValue { Value: T value } && predicate(value); - } -} \ No newline at end of file diff --git a/EntrustSettle.Serilog/Sink/LogBatchingSink.cs b/EntrustSettle.Serilog/Sink/LogBatchingSink.cs deleted file mode 100644 index ae28dcc..0000000 --- a/EntrustSettle.Serilog/Sink/LogBatchingSink.cs +++ /dev/null @@ -1,135 +0,0 @@ -using EntrustSettle.Common; -using EntrustSettle.Model.Logs; -using EntrustSettle.Serilog.Extensions; -using Mapster; -using Serilog.Events; -using Serilog.Sinks.PeriodicBatching; -using SqlSugar; - -namespace EntrustSettle.Serilog.Sink; - -public class LogBatchingSink : IBatchedLogEventSink -{ - public async Task EmitBatchAsync(IEnumerable batch) - { - var sugar = App.GetService(false); - - await WriteSqlLog(sugar, batch.FilterSqlLog()); - await WriteLogs(sugar, batch.FilterRemoveOtherLog()); - } - - public Task OnEmptyBatchAsync() - { - return Task.CompletedTask; - } - - #region Write Log - - private async Task WriteLogs(ISqlSugarClient db, IEnumerable batch) - { - if (!batch.Any()) - { - return; - } - - var group = batch.GroupBy(s => s.Level); - foreach (var v in group) - { - switch (v.Key) - { - case LogEventLevel.Information: - await WriteInformationLog(db, v); - break; - case LogEventLevel.Warning: - await WriteWarningLog(db, v); - break; - case LogEventLevel.Error: - case LogEventLevel.Fatal: - await WriteErrorLog(db, v); - break; - } - } - } - - private async Task WriteInformationLog(ISqlSugarClient db, IEnumerable batch) - { - if (!batch.Any()) - { - return; - } - - var logs = new List(); - foreach (var logEvent in batch) - { - var log = logEvent.Adapt(); - log.Message = logEvent.RenderMessage(); - log.Properties = logEvent.Properties.ToJson(); - log.DateTime = logEvent.Timestamp.DateTime; - logs.Add(log); - } - - await db.AsTenant().InsertableWithAttr(logs).SplitTable().ExecuteReturnSnowflakeIdAsync(); - } - - private async Task WriteWarningLog(ISqlSugarClient db, IEnumerable batch) - { - if (!batch.Any()) - { - return; - } - - var logs = new List(); - foreach (var logEvent in batch) - { - var log = logEvent.Adapt(); - log.Message = logEvent.RenderMessage(); - log.Properties = logEvent.Properties.ToJson(); - log.DateTime = logEvent.Timestamp.DateTime; - logs.Add(log); - } - - await db.AsTenant().InsertableWithAttr(logs).SplitTable().ExecuteReturnSnowflakeIdAsync(); - } - - private async Task WriteErrorLog(ISqlSugarClient db, IEnumerable batch) - { - if (!batch.Any()) - { - return; - } - - var logs = new List(); - foreach (var logEvent in batch) - { - var log = logEvent.Adapt(); - log.Message = logEvent.RenderMessage(); - log.Properties = logEvent.Properties.ToJson(); - log.DateTime = logEvent.Timestamp.DateTime; - logs.Add(log); - } - - await db.AsTenant().InsertableWithAttr(logs).SplitTable().ExecuteReturnSnowflakeIdAsync(); - } - - private async Task WriteSqlLog(ISqlSugarClient db, IEnumerable batch) - { - if (!batch.Any()) - { - return; - } - - var logs = new List(); - foreach (var logEvent in batch) - { - var log = logEvent.Adapt(); - log.Message = logEvent.RenderMessage(); - log.Properties = logEvent.Properties.ToJson(); - log.DateTime = logEvent.Timestamp.DateTime; - logs.Add(log); - } - - await db.AsTenant().InsertableWithAttr(logs).SplitTable().ExecuteReturnSnowflakeIdAsync(); - } - - #endregion -} \ No newline at end of file diff --git a/EntrustSettle.Serilog/Utility/SerilogRequestUtility.cs b/EntrustSettle.Serilog/Utility/SerilogRequestUtility.cs deleted file mode 100644 index 56d4b34..0000000 --- a/EntrustSettle.Serilog/Utility/SerilogRequestUtility.cs +++ /dev/null @@ -1,73 +0,0 @@ -using EntrustSettle.Common.Extensions; -using EntrustSettle.Common.Http; -using Microsoft.AspNetCore.Http; -using Serilog; -using Serilog.Events; - -namespace EntrustSettle.Serilog.Utility; - -public class SerilogRequestUtility -{ - public const string HttpMessageTemplate = - "HTTP {RequestMethod} {RequestPath} QueryString:{QueryString} Body:{Body} responded {StatusCode} in {Elapsed:0.0000} ms"; - - private static readonly List _ignoreUrl = new() - { - "/job", - }; - - private static LogEventLevel DefaultGetLevel(HttpContext ctx, - double _, - Exception ex) - { - return ex is null && ctx.Response.StatusCode <= 499 ? LogEventLevel.Information : LogEventLevel.Error; - } - - public static LogEventLevel GetRequestLevel(HttpContext ctx, double _, Exception ex) => - ex is null && ctx.Response.StatusCode <= 499 ? IgnoreRequest(ctx) : LogEventLevel.Error; - - private static LogEventLevel IgnoreRequest(HttpContext ctx) - { - var path = ctx.Request.Path.Value; - if (path.IsNullOrEmpty()) - { - return LogEventLevel.Information; - } - - return _ignoreUrl.Any(s => path.StartsWith(s)) ? LogEventLevel.Verbose : LogEventLevel.Information; - } - - /// - /// 从Request中增加附属属性 - /// - /// - /// - public static void EnrichFromRequest(IDiagnosticContext diagnosticContext, HttpContext httpContext) - { - var request = httpContext.Request; - - diagnosticContext.Set("RequestHost", request.Host); - diagnosticContext.Set("RequestScheme", request.Scheme); - diagnosticContext.Set("Protocol", request.Protocol); - diagnosticContext.Set("RequestIp", httpContext.GetRequestIp()); - - if (request.Method == HttpMethods.Get) - { - diagnosticContext.Set("QueryString", request.QueryString.HasValue ? request.QueryString.Value : string.Empty); - diagnosticContext.Set("Body", string.Empty); - } - else - { - diagnosticContext.Set("QueryString", request.QueryString.HasValue ? request.QueryString.Value : string.Empty); - diagnosticContext.Set("Body", request.ContentLength > 0 ? request.GetRequestBody() : string.Empty); - } - - diagnosticContext.Set("ContentType", httpContext.Response.ContentType); - - var endpoint = httpContext.GetEndpoint(); - if (endpoint != null) - { - diagnosticContext.Set("EndpointName", endpoint.DisplayName); - } - } -} \ No newline at end of file diff --git a/EntrustSettle.Tasks/QuartzNet/Jobs/JobHydStatusQuartz.cs b/EntrustSettle.Tasks/QuartzNet/Jobs/JobHydStatusQuartz.cs index 5534e2a..d4b6687 100644 --- a/EntrustSettle.Tasks/QuartzNet/Jobs/JobHydStatusQuartz.cs +++ b/EntrustSettle.Tasks/QuartzNet/Jobs/JobHydStatusQuartz.cs @@ -104,7 +104,7 @@ namespace EntrustSettle.Tasks // 将更新后的状态及费用推送到消息队列 if (AppSettings.app("RabbitMQ", "Enabled").ObjToBool()) { - _ = Task.Run(() => + _ = Task.Run(async () => { string msg = $"Id:[{order.Id}],提单号:[{order.Mblno}],自动更新状态后推送队列"; try @@ -127,16 +127,16 @@ namespace EntrustSettle.Tasks _ => "未知状态", } }; - //var feeList = await orderFeeService.Query(x => x.OrderId == order.Id); - //if (feeList.Count > 0) - //{ - // pushDto.FeeList = feeList.Select(x => new StatusPushDto.FeeDto() - // { - // Id = x.Id, - // FeeName = x.Name, - // FeeAmount = x.Amount - // }).ToList(); - //} + var feeList = await orderFeeService.Query(x => x.OrderId == order.Id); + if (feeList.Count > 0) + { + pushDto.FeeList = feeList.Select(x => new StatusPushDto.FeeDto() + { + FeeId = x.Id, + FeeName = x.Name, + FeeAmount = x.Amount + }).ToList(); + } var json = JsonConvert.SerializeObject(pushDto, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); queueService.Push(msg, order.CompanyId, json);