You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
2.2 KiB
67 lines
2.2 KiB
3 years ago
|
using Microsoft.Extensions.DependencyInjection;
|
||
|
using Microsoft.Extensions.Logging;
|
||
|
using Microsoft.Extensions.Logging.Abstractions;
|
||
|
using Microsoft.Extensions.Options;
|
||
|
using System;
|
||
|
using System.Threading.Tasks;
|
||
|
using Volo.Abp.ExceptionHandling;
|
||
|
|
||
|
namespace Sanhe.Abp.ExceptionHandling;
|
||
|
|
||
|
public abstract class AbpExceptionSubscriberBase : ExceptionSubscriber
|
||
|
{
|
||
|
protected IServiceScopeFactory ServiceScopeFactory { get; }
|
||
|
protected AbpExceptionHandlingOptions Options { get; }
|
||
|
protected IServiceProvider ServiceProvider { get; set; }
|
||
|
protected readonly object ServiceProviderLock = new();
|
||
|
|
||
|
protected TService LazyGetRequiredService<TService>(ref TService reference)
|
||
|
=> LazyGetRequiredService(typeof(TService), ref reference);
|
||
|
|
||
|
protected TRef LazyGetRequiredService<TRef>(Type serviceType, ref TRef reference)
|
||
|
{
|
||
|
if (reference == null)
|
||
|
{
|
||
|
lock (ServiceProviderLock)
|
||
|
{
|
||
|
if (reference == null)
|
||
|
{
|
||
|
reference = (TRef)ServiceProvider.GetRequiredService(serviceType);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return reference;
|
||
|
}
|
||
|
|
||
|
protected ILoggerFactory LoggerFactory => LazyGetRequiredService(ref _loggerFactory);
|
||
|
private ILoggerFactory _loggerFactory;
|
||
|
|
||
|
protected ILogger Logger => _lazyLogger.Value;
|
||
|
private Lazy<ILogger> _lazyLogger => new(() => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance, true);
|
||
|
|
||
|
|
||
|
protected AbpExceptionSubscriberBase(
|
||
|
IServiceScopeFactory serviceScopeFactory,
|
||
|
IOptions<AbpExceptionHandlingOptions> options)
|
||
|
{
|
||
|
Options = options.Value;
|
||
|
ServiceScopeFactory = serviceScopeFactory;
|
||
|
}
|
||
|
|
||
|
public override async Task HandleAsync(ExceptionNotificationContext context)
|
||
|
{
|
||
|
if (context.Handled &&
|
||
|
Options.HasNotifierError(context.Exception))
|
||
|
{
|
||
|
using (var scope = ServiceScopeFactory.CreateScope())
|
||
|
{
|
||
|
await SendErrorNotifierAsync(
|
||
|
new ExceptionSendNotifierContext(scope.ServiceProvider, context.Exception, context.LogLevel));
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected abstract Task SendErrorNotifierAsync(ExceptionSendNotifierContext context);
|
||
|
}
|