|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using Microsoft.Extensions.DependencyInjection; |
| 4 | +using Microsoft.Extensions.Logging; |
| 5 | +using Shouldly; |
| 6 | +using Volo.Abp.AspNetCore.Components.ExceptionHandling; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace Volo.Abp.AspNetCore.Components.Web.ExceptionHandling; |
| 10 | + |
| 11 | +public class AbpExceptionHandlingLogger_Tests |
| 12 | +{ |
| 13 | + [Fact] |
| 14 | + public void Should_Not_Recurse_When_Informer_Writes_To_Same_Logger_Pipeline() |
| 15 | + { |
| 16 | + var services = new ServiceCollection(); |
| 17 | + var informer = new RecordingUserExceptionInformer(); |
| 18 | + services.AddSingleton<IUserExceptionInformer>(informer); |
| 19 | + |
| 20 | + var provider = new AbpExceptionHandlingLoggerProvider(services); |
| 21 | + services.AddLogging(builder => builder |
| 22 | + .AddProvider(provider) |
| 23 | + .AddFilter<AbpExceptionHandlingLoggerProvider>(typeof(UserExceptionInformer).FullName, LogLevel.None)); |
| 24 | + |
| 25 | + var accessor = services.AddObjectAccessor<IServiceProvider>(); |
| 26 | + var sp = services.BuildServiceProvider(); |
| 27 | + accessor.Value = sp; |
| 28 | + |
| 29 | + informer.Logger = sp.GetRequiredService<ILoggerFactory>() |
| 30 | + .CreateLogger(typeof(UserExceptionInformer).FullName!); |
| 31 | + |
| 32 | + var entryLogger = sp.GetRequiredService<ILogger<AbpExceptionHandlingLogger_Tests>>(); |
| 33 | + entryLogger.LogError(new InvalidOperationException("boom"), "entry"); |
| 34 | + |
| 35 | + informer.InformCalls.ShouldBe(1); |
| 36 | + } |
| 37 | + |
| 38 | + private sealed class RecordingUserExceptionInformer : IUserExceptionInformer |
| 39 | + { |
| 40 | + private const int RecursionCircuitBreaker = 50; |
| 41 | + |
| 42 | + public int InformCalls; |
| 43 | + public ILogger? Logger; |
| 44 | + |
| 45 | + public void Inform(UserExceptionInformerContext context) |
| 46 | + { |
| 47 | + InformCalls++; |
| 48 | + if (InformCalls > RecursionCircuitBreaker) |
| 49 | + { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + Logger?.LogError(context.Exception, context.Exception.Message); |
| 54 | + } |
| 55 | + |
| 56 | + public Task InformAsync(UserExceptionInformerContext context) |
| 57 | + { |
| 58 | + Inform(context); |
| 59 | + return Task.CompletedTask; |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments