mirror of https://github.com/abpframework/abp.git
committed by
GitHub
8 changed files with 184 additions and 14 deletions
@ -1,14 +1,34 @@ |
|||||
using System; |
using System; |
||||
using System.Collections.Generic; |
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
|
||||
namespace Volo.Abp.Logging; |
namespace Volo.Abp.Logging; |
||||
|
|
||||
public class DefaultInitLoggerFactory : IInitLoggerFactory |
public class DefaultInitLoggerFactory : IInitLoggerFactory |
||||
{ |
{ |
||||
private readonly Dictionary<Type, object> _cache = []; |
private readonly Dictionary<Type, object> _cache = []; |
||||
|
private readonly List<List<AbpInitLogEntry>> _entryLists = []; |
||||
|
|
||||
public virtual IInitLogger<T> Create<T>() |
public virtual IInitLogger<T> Create<T>() |
||||
{ |
{ |
||||
return (IInitLogger<T>)_cache.GetOrAdd(typeof(T), () => new DefaultInitLogger<T>()); |
return (IInitLogger<T>)_cache.GetOrAdd(typeof(T), () => |
||||
|
{ |
||||
|
var logger = new DefaultInitLogger<T>(); |
||||
|
_entryLists.Add(logger.Entries); |
||||
|
return logger; |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
public virtual List<AbpInitLogEntry> GetAllEntries() |
||||
|
{ |
||||
|
return _entryLists.SelectMany(l => l).ToList(); |
||||
|
} |
||||
|
|
||||
|
public virtual void ClearAllEntries() |
||||
|
{ |
||||
|
foreach (var list in _entryLists) |
||||
|
{ |
||||
|
list.Clear(); |
||||
|
} |
||||
} |
} |
||||
} |
} |
||||
|
|||||
@ -1,6 +1,12 @@ |
|||||
namespace Volo.Abp.Logging; |
using System.Collections.Generic; |
||||
|
|
||||
|
namespace Volo.Abp.Logging; |
||||
|
|
||||
public interface IInitLoggerFactory |
public interface IInitLoggerFactory |
||||
{ |
{ |
||||
IInitLogger<T> Create<T>(); |
IInitLogger<T> Create<T>(); |
||||
|
|
||||
|
List<AbpInitLogEntry> GetAllEntries(); |
||||
|
|
||||
|
void ClearAllEntries(); |
||||
} |
} |
||||
|
|||||
@ -0,0 +1,62 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Microsoft.Extensions.Logging; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.Logging; |
||||
|
using Volo.Abp.Modularity; |
||||
|
using Volo.Abp.Testing; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.Autofac; |
||||
|
|
||||
|
public class WarnForOrphanedAbpModules_Tests : AbpIntegratedTest<WarnForOrphanedAbpModules_Tests.TestModule> |
||||
|
{ |
||||
|
private static readonly Type OrphanModuleType = typeof(AbpTestModule); |
||||
|
private List<AbpInitLogEntry> _initLogEntries = []; |
||||
|
|
||||
|
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) |
||||
|
{ |
||||
|
options.UseAutofac(); |
||||
|
} |
||||
|
|
||||
|
protected override IServiceProvider CreateServiceProvider(IServiceCollection services) |
||||
|
{ |
||||
|
var serviceProvider = base.CreateServiceProvider(services); |
||||
|
|
||||
|
// Capture init log entries after Autofac Populate/Register but before WriteInitLogs clears them.
|
||||
|
_initLogEntries = serviceProvider.GetRequiredService<IInitLoggerFactory>().GetAllEntries(); |
||||
|
|
||||
|
return serviceProvider; |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Warn_For_Orphaned_Abp_Modules() |
||||
|
{ |
||||
|
_initLogEntries |
||||
|
.Where(e => e.LogLevel == LogLevel.Warning) |
||||
|
.ShouldContain(e => e.Message.Contains(OrphanModuleType.FullName!), |
||||
|
$"Expected a warning for orphaned module '{OrphanModuleType.FullName}'."); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Not_Warn_For_Loaded_Modules() |
||||
|
{ |
||||
|
_initLogEntries |
||||
|
.Where(e => e.LogLevel == LogLevel.Warning) |
||||
|
.ShouldNotContain(e => e.Message.Contains(typeof(AbpAutofacModule).FullName!), |
||||
|
"Modules in the [DependsOn] chain should not be reported as orphaned."); |
||||
|
} |
||||
|
|
||||
|
[DependsOn(typeof(AbpAutofacModule))] |
||||
|
public class TestModule : AbpModule |
||||
|
{ |
||||
|
public override void ConfigureServices(ServiceConfigurationContext context) |
||||
|
{ |
||||
|
// Simulate ASP.NET Core's AddControllersAsServices() registering a type
|
||||
|
// from an assembly whose ABP module is NOT in the [DependsOn] chain.
|
||||
|
context.Services.AddTransient<AbpTestModule>(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue