From 592b511614ffb319a65e9f415cf32d89bb401d7f Mon Sep 17 00:00:00 2001 From: maliming <6908465+maliming@users.noreply.github.com> Date: Tue, 4 Aug 2020 10:42:13 +0800 Subject: [PATCH] Combine related log messages to ones. Resolve #4949 --- .../Mvc/ExceptionHandling/AbpExceptionFilter.cs | 12 ++++++++---- .../ExceptionHandling/AbpExceptionPageFilter.cs | 8 ++++++-- .../Extensions/Logging/AbpLoggerExtensions.cs | 9 ++++++--- .../Volo/Abp/Validation/AbpValidationException.cs | 14 +++++++++----- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs index 1ce177ba44..009d52b42f 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs @@ -1,4 +1,5 @@ using System; +using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -25,7 +26,7 @@ namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling public AbpExceptionFilter( IExceptionToErrorInfoConverter errorInfoConverter, - IHttpExceptionStatusCodeFinder statusCodeFinder, + IHttpExceptionStatusCodeFinder statusCodeFinder, IJsonSerializer jsonSerializer) { _errorInfoConverter = errorInfoConverter; @@ -54,7 +55,7 @@ namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling { return true; } - + if (context.HttpContext.Request.CanAccept(MimeTypes.Application.Json)) { return true; @@ -81,8 +82,11 @@ namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling var logLevel = context.Exception.GetLogLevel(); - Logger.LogWithLevel(logLevel, $"---------- {nameof(RemoteServiceErrorInfo)} ----------"); - Logger.LogWithLevel(logLevel, _jsonSerializer.Serialize(remoteServiceErrorInfo, indented: true)); + var remoteServiceErrorInfoBuilder = new StringBuilder(); + remoteServiceErrorInfoBuilder.AppendLine($"---------- {nameof(RemoteServiceErrorInfo)} ----------"); + remoteServiceErrorInfoBuilder.AppendLine( _jsonSerializer.Serialize(remoteServiceErrorInfo, indented: true)); + Logger.LogWithLevel(logLevel, remoteServiceErrorInfoBuilder.ToString()); + Logger.LogException(context.Exception, logLevel); await context.HttpContext diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs index 9e449f0474..8e2356ff20 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs @@ -1,4 +1,5 @@ using System; +using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -93,8 +94,11 @@ namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling var logLevel = context.Exception.GetLogLevel(); - Logger.LogWithLevel(logLevel, $"---------- {nameof(RemoteServiceErrorInfo)} ----------"); - Logger.LogWithLevel(logLevel, _jsonSerializer.Serialize(remoteServiceErrorInfo, indented: true)); + var remoteServiceErrorInfoBuilder = new StringBuilder(); + remoteServiceErrorInfoBuilder.AppendLine($"---------- {nameof(RemoteServiceErrorInfo)} ----------"); + remoteServiceErrorInfoBuilder.AppendLine( _jsonSerializer.Serialize(remoteServiceErrorInfo, indented: true)); + Logger.LogWithLevel(logLevel, remoteServiceErrorInfoBuilder.ToString()); + Logger.LogException(context.Exception, logLevel); await context.HttpContext diff --git a/framework/src/Volo.Abp.Core/Microsoft/Extensions/Logging/AbpLoggerExtensions.cs b/framework/src/Volo.Abp.Core/Microsoft/Extensions/Logging/AbpLoggerExtensions.cs index a711271cd2..5d975b2e58 100644 --- a/framework/src/Volo.Abp.Core/Microsoft/Extensions/Logging/AbpLoggerExtensions.cs +++ b/framework/src/Volo.Abp.Core/Microsoft/Extensions/Logging/AbpLoggerExtensions.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Text; using Volo.Abp.ExceptionHandling; using Volo.Abp.Logging; @@ -87,12 +88,14 @@ namespace Microsoft.Extensions.Logging return; } - logger.LogWithLevel(logLevel, "---------- Exception Data ----------"); - + var exceptionData = new StringBuilder(); + exceptionData.AppendLine("---------- Exception Data ----------"); foreach (var key in exception.Data.Keys) { - logger.LogWithLevel(logLevel, $"{key} = {exception.Data[key]}"); + exceptionData.AppendLine($"{key} = {exception.Data[key]}"); } + + logger.LogWithLevel(logLevel, exceptionData.ToString()); } private static void LogSelfLogging(ILogger logger, Exception exception) diff --git a/framework/src/Volo.Abp.Validation.Abstractions/Volo/Abp/Validation/AbpValidationException.cs b/framework/src/Volo.Abp.Validation.Abstractions/Volo/Abp/Validation/AbpValidationException.cs index fb3be504c5..5781eaecca 100644 --- a/framework/src/Volo.Abp.Validation.Abstractions/Volo/Abp/Validation/AbpValidationException.cs +++ b/framework/src/Volo.Abp.Validation.Abstractions/Volo/Abp/Validation/AbpValidationException.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Runtime.Serialization; +using System.Text; using Microsoft.Extensions.Logging; using Volo.Abp.Logging; @@ -12,9 +13,9 @@ namespace Volo.Abp.Validation /// This exception type is used to throws validation exceptions. /// [Serializable] - public class AbpValidationException : AbpException, - IHasLogLevel, - IHasValidationErrors, + public class AbpValidationException : AbpException, + IHasLogLevel, + IHasValidationErrors, IExceptionWithSelfLogging { /// @@ -99,7 +100,8 @@ namespace Volo.Abp.Validation return; } - logger.LogWithLevel(LogLevel, "There are " + ValidationErrors.Count + " validation errors:"); + var validationErrors = new StringBuilder(); + validationErrors.AppendLine("There are " + ValidationErrors.Count + " validation errors:"); foreach (var validationResult in ValidationErrors) { var memberNames = ""; @@ -108,8 +110,10 @@ namespace Volo.Abp.Validation memberNames = " (" + string.Join(", ", validationResult.MemberNames) + ")"; } - logger.LogWithLevel(LogLevel, validationResult.ErrorMessage + memberNames); + validationErrors.AppendLine(validationResult.ErrorMessage + memberNames); } + + logger.LogWithLevel(LogLevel, validationErrors.ToString()); } } }