| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- using System;
- using System.Linq;
- using Abp.Collections.Extensions;
- using Abp.Dependency;
- using Abp.Runtime.Validation;
- using Castle.Core.Logging;
- namespace Abp.Logging
- {
- /// <summary>
- /// This class can be used to write logs from somewhere where it's a hard to get a reference to the <see cref="ILogger"/>.
- /// Normally, use <see cref="ILogger"/> with property injection wherever it's possible.
- /// </summary>
- public static class LogHelper
- {
- /// <summary>
- /// A reference to the logger.
- /// </summary>
- public static ILogger Logger { get; private set; }
- static LogHelper()
- {
- Logger = IocManager.Instance.IsRegistered(typeof(ILoggerFactory))
- ? IocManager.Instance.Resolve<ILoggerFactory>().Create(typeof(LogHelper))
- : NullLogger.Instance;
- }
- public static void LogException(Exception ex)
- {
- LogException(Logger, ex);
- }
- public static void LogException(ILogger logger, Exception ex)
- {
- var severity = (ex as IHasLogSeverity)?.Severity ?? LogSeverity.Error;
- logger.Log(severity, ex.Message, ex);
- LogValidationErrors(logger, ex);
- }
- private static void LogValidationErrors(ILogger logger, Exception exception)
- {
- //Try to find inner validation exception
- if (exception is AggregateException && exception.InnerException != null)
- {
- var aggException = exception as AggregateException;
- if (aggException.InnerException is AbpValidationException)
- {
- exception = aggException.InnerException;
- }
- }
- if (!(exception is AbpValidationException))
- {
- return;
- }
- var validationException = exception as AbpValidationException;
- if (validationException.ValidationErrors.IsNullOrEmpty())
- {
- return;
- }
- logger.Log(validationException.Severity, "There are " + validationException.ValidationErrors.Count + " validation errors:");
- foreach (var validationResult in validationException.ValidationErrors)
- {
- var memberNames = "";
- if (validationResult.MemberNames != null && validationResult.MemberNames.Any())
- {
- memberNames = " (" + string.Join(", ", validationResult.MemberNames) + ")";
- }
- logger.Log(validationException.Severity, validationResult.ErrorMessage + memberNames);
- }
- }
- }
- }
|