Capture LoggedTest.Initialize exception and re-trow in Dispose (#770)
This commit is contained in:
parent
e7cca176e5
commit
688914bb70
|
|
@ -5,6 +5,7 @@ using System;
|
|||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.ExceptionServices;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
|
|
@ -12,6 +13,8 @@ namespace Microsoft.Extensions.Logging.Testing
|
|||
{
|
||||
public class LoggedTestBase : ILoggedTest
|
||||
{
|
||||
private ExceptionDispatchInfo _initializationException;
|
||||
|
||||
private IDisposable _testLog;
|
||||
|
||||
// Obsolete but keeping for back compat
|
||||
|
|
@ -48,37 +51,48 @@ namespace Microsoft.Extensions.Logging.Testing
|
|||
|
||||
public virtual void Initialize(MethodInfo methodInfo, object[] testMethodArguments, ITestOutputHelper testOutputHelper)
|
||||
{
|
||||
TestOutputHelper = testOutputHelper;
|
||||
try
|
||||
{
|
||||
TestOutputHelper = testOutputHelper;
|
||||
|
||||
var classType = GetType();
|
||||
var logLevelAttribute = methodInfo.GetCustomAttribute<LogLevelAttribute>()
|
||||
?? methodInfo.DeclaringType.GetCustomAttribute<LogLevelAttribute>()
|
||||
?? methodInfo.DeclaringType.Assembly.GetCustomAttribute<LogLevelAttribute>();
|
||||
var testName = testMethodArguments.Aggregate(methodInfo.Name, (a, b) => $"{a}-{(b ?? "null")}");
|
||||
var classType = GetType();
|
||||
var logLevelAttribute = methodInfo.GetCustomAttribute<LogLevelAttribute>()
|
||||
?? methodInfo.DeclaringType.GetCustomAttribute<LogLevelAttribute>()
|
||||
?? methodInfo.DeclaringType.Assembly.GetCustomAttribute<LogLevelAttribute>();
|
||||
var testName = testMethodArguments.Aggregate(methodInfo.Name, (a, b) => $"{a}-{(b ?? "null")}");
|
||||
|
||||
var useShortClassName = methodInfo.DeclaringType.GetCustomAttribute<ShortClassNameAttribute>()
|
||||
?? methodInfo.DeclaringType.Assembly.GetCustomAttribute<ShortClassNameAttribute>();
|
||||
// internal for testing
|
||||
ResolvedTestClassName = useShortClassName == null ? classType.FullName : classType.Name;
|
||||
var useShortClassName = methodInfo.DeclaringType.GetCustomAttribute<ShortClassNameAttribute>()
|
||||
?? methodInfo.DeclaringType.Assembly.GetCustomAttribute<ShortClassNameAttribute>();
|
||||
// internal for testing
|
||||
ResolvedTestClassName = useShortClassName == null ? classType.FullName : classType.Name;
|
||||
|
||||
_testLog = AssemblyTestLog
|
||||
.ForAssembly(classType.GetTypeInfo().Assembly)
|
||||
.StartTestLog(
|
||||
TestOutputHelper,
|
||||
ResolvedTestClassName,
|
||||
out var loggerFactory,
|
||||
logLevelAttribute?.LogLevel ?? LogLevel.Debug,
|
||||
out var resolvedTestName,
|
||||
out var logOutputDirectory,
|
||||
testName);
|
||||
_testLog = AssemblyTestLog
|
||||
.ForAssembly(classType.GetTypeInfo().Assembly)
|
||||
.StartTestLog(
|
||||
TestOutputHelper,
|
||||
ResolvedTestClassName,
|
||||
out var loggerFactory,
|
||||
logLevelAttribute?.LogLevel ?? LogLevel.Debug,
|
||||
out var resolvedTestName,
|
||||
out var logOutputDirectory,
|
||||
testName);
|
||||
|
||||
ResolvedLogOutputDirectory = logOutputDirectory;
|
||||
ResolvedTestMethodName = resolvedTestName;
|
||||
ResolvedLogOutputDirectory = logOutputDirectory;
|
||||
ResolvedTestMethodName = resolvedTestName;
|
||||
|
||||
LoggerFactory = loggerFactory;
|
||||
Logger = loggerFactory.CreateLogger(classType);
|
||||
LoggerFactory = loggerFactory;
|
||||
Logger = loggerFactory.CreateLogger(classType);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_initializationException = ExceptionDispatchInfo.Capture(e);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Dispose() => _testLog.Dispose();
|
||||
public virtual void Dispose()
|
||||
{
|
||||
_initializationException?.Throw();
|
||||
_testLog.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue