mirror of https://github.com/abpframework/abp.git
Browse Source
Implemented: Allow to subscribe to exceptions handled by the abp frameworkpull/3336/head
committed by
GitHub
20 changed files with 314 additions and 40 deletions
@ -0,0 +1,32 @@ |
|||||
|
using System; |
||||
|
using JetBrains.Annotations; |
||||
|
using Microsoft.Extensions.Logging; |
||||
|
|
||||
|
namespace Volo.Abp.ExceptionHandling |
||||
|
{ |
||||
|
public class ExceptionNotificationContext |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// The exception object.
|
||||
|
/// </summary>
|
||||
|
[NotNull] |
||||
|
public Exception Exception { get; } |
||||
|
|
||||
|
public LogLevel LogLevel { get; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// True, if it is handled.
|
||||
|
/// </summary>
|
||||
|
public bool Handled { get; } |
||||
|
|
||||
|
public ExceptionNotificationContext( |
||||
|
[NotNull] Exception exception, |
||||
|
LogLevel? logLevel = null, |
||||
|
bool handled = true) |
||||
|
{ |
||||
|
Exception = Check.NotNull(exception, nameof(exception)); |
||||
|
LogLevel = logLevel ?? exception.GetLogLevel(); |
||||
|
Handled = handled; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,41 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Threading.Tasks; |
||||
|
using JetBrains.Annotations; |
||||
|
using Microsoft.Extensions.Logging; |
||||
|
using Microsoft.Extensions.Logging.Abstractions; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.ExceptionHandling |
||||
|
{ |
||||
|
public class ExceptionNotifier : IExceptionNotifier, ITransientDependency |
||||
|
{ |
||||
|
public ILogger<ExceptionNotifier> Logger { get; set; } |
||||
|
|
||||
|
protected IEnumerable<IExceptionSubscriber> ExceptionSubscribers { get; } |
||||
|
|
||||
|
public ExceptionNotifier(IEnumerable<IExceptionSubscriber> exceptionSubscribers) |
||||
|
{ |
||||
|
ExceptionSubscribers = exceptionSubscribers; |
||||
|
Logger = NullLogger<ExceptionNotifier>.Instance; |
||||
|
} |
||||
|
|
||||
|
public virtual async Task NotifyAsync([NotNull] ExceptionNotificationContext context) |
||||
|
{ |
||||
|
Check.NotNull(context, nameof(context)); |
||||
|
|
||||
|
foreach (var exceptionSubscriber in ExceptionSubscribers) |
||||
|
{ |
||||
|
try |
||||
|
{ |
||||
|
await exceptionSubscriber.HandleAsync(context); |
||||
|
} |
||||
|
catch (Exception e) |
||||
|
{ |
||||
|
Logger.LogWarning($"Exception subscriber of type {exceptionSubscriber.GetType().AssemblyQualifiedName} has thrown an exception!"); |
||||
|
Logger.LogException(e, LogLevel.Warning); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,27 @@ |
|||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using JetBrains.Annotations; |
||||
|
using Microsoft.Extensions.Logging; |
||||
|
|
||||
|
namespace Volo.Abp.ExceptionHandling |
||||
|
{ |
||||
|
public static class ExceptionNotifierExtensions |
||||
|
{ |
||||
|
public static Task NotifyAsync( |
||||
|
[NotNull] this IExceptionNotifier exceptionNotifier, |
||||
|
[NotNull] Exception exception, |
||||
|
LogLevel? logLevel = null, |
||||
|
bool handled = true) |
||||
|
{ |
||||
|
Check.NotNull(exceptionNotifier, nameof(exceptionNotifier)); |
||||
|
|
||||
|
return exceptionNotifier.NotifyAsync( |
||||
|
new ExceptionNotificationContext( |
||||
|
exception, |
||||
|
logLevel, |
||||
|
handled |
||||
|
) |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,11 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.ExceptionHandling |
||||
|
{ |
||||
|
[ExposeServices(typeof(IExceptionSubscriber))] |
||||
|
public abstract class ExceptionSubscriber : IExceptionSubscriber, ITransientDependency |
||||
|
{ |
||||
|
public abstract Task HandleAsync(ExceptionNotificationContext context); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,10 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using JetBrains.Annotations; |
||||
|
|
||||
|
namespace Volo.Abp.ExceptionHandling |
||||
|
{ |
||||
|
public interface IExceptionNotifier |
||||
|
{ |
||||
|
Task NotifyAsync([NotNull] ExceptionNotificationContext context); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,10 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using JetBrains.Annotations; |
||||
|
|
||||
|
namespace Volo.Abp.ExceptionHandling |
||||
|
{ |
||||
|
public interface IExceptionSubscriber |
||||
|
{ |
||||
|
Task HandleAsync([NotNull] ExceptionNotificationContext context); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue