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.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.ExceptionServices;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Xunit.Abstractions;
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
|
|
@ -12,6 +13,8 @@ namespace Microsoft.Extensions.Logging.Testing
|
||||||
{
|
{
|
||||||
public class LoggedTestBase : ILoggedTest
|
public class LoggedTestBase : ILoggedTest
|
||||||
{
|
{
|
||||||
|
private ExceptionDispatchInfo _initializationException;
|
||||||
|
|
||||||
private IDisposable _testLog;
|
private IDisposable _testLog;
|
||||||
|
|
||||||
// Obsolete but keeping for back compat
|
// 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)
|
public virtual void Initialize(MethodInfo methodInfo, object[] testMethodArguments, ITestOutputHelper testOutputHelper)
|
||||||
{
|
{
|
||||||
TestOutputHelper = testOutputHelper;
|
try
|
||||||
|
{
|
||||||
|
TestOutputHelper = testOutputHelper;
|
||||||
|
|
||||||
var classType = GetType();
|
var classType = GetType();
|
||||||
var logLevelAttribute = methodInfo.GetCustomAttribute<LogLevelAttribute>()
|
var logLevelAttribute = methodInfo.GetCustomAttribute<LogLevelAttribute>()
|
||||||
?? methodInfo.DeclaringType.GetCustomAttribute<LogLevelAttribute>()
|
?? methodInfo.DeclaringType.GetCustomAttribute<LogLevelAttribute>()
|
||||||
?? methodInfo.DeclaringType.Assembly.GetCustomAttribute<LogLevelAttribute>();
|
?? methodInfo.DeclaringType.Assembly.GetCustomAttribute<LogLevelAttribute>();
|
||||||
var testName = testMethodArguments.Aggregate(methodInfo.Name, (a, b) => $"{a}-{(b ?? "null")}");
|
var testName = testMethodArguments.Aggregate(methodInfo.Name, (a, b) => $"{a}-{(b ?? "null")}");
|
||||||
|
|
||||||
var useShortClassName = methodInfo.DeclaringType.GetCustomAttribute<ShortClassNameAttribute>()
|
var useShortClassName = methodInfo.DeclaringType.GetCustomAttribute<ShortClassNameAttribute>()
|
||||||
?? methodInfo.DeclaringType.Assembly.GetCustomAttribute<ShortClassNameAttribute>();
|
?? methodInfo.DeclaringType.Assembly.GetCustomAttribute<ShortClassNameAttribute>();
|
||||||
// internal for testing
|
// internal for testing
|
||||||
ResolvedTestClassName = useShortClassName == null ? classType.FullName : classType.Name;
|
ResolvedTestClassName = useShortClassName == null ? classType.FullName : classType.Name;
|
||||||
|
|
||||||
_testLog = AssemblyTestLog
|
_testLog = AssemblyTestLog
|
||||||
.ForAssembly(classType.GetTypeInfo().Assembly)
|
.ForAssembly(classType.GetTypeInfo().Assembly)
|
||||||
.StartTestLog(
|
.StartTestLog(
|
||||||
TestOutputHelper,
|
TestOutputHelper,
|
||||||
ResolvedTestClassName,
|
ResolvedTestClassName,
|
||||||
out var loggerFactory,
|
out var loggerFactory,
|
||||||
logLevelAttribute?.LogLevel ?? LogLevel.Debug,
|
logLevelAttribute?.LogLevel ?? LogLevel.Debug,
|
||||||
out var resolvedTestName,
|
out var resolvedTestName,
|
||||||
out var logOutputDirectory,
|
out var logOutputDirectory,
|
||||||
testName);
|
testName);
|
||||||
|
|
||||||
ResolvedLogOutputDirectory = logOutputDirectory;
|
ResolvedLogOutputDirectory = logOutputDirectory;
|
||||||
ResolvedTestMethodName = resolvedTestName;
|
ResolvedTestMethodName = resolvedTestName;
|
||||||
|
|
||||||
LoggerFactory = loggerFactory;
|
LoggerFactory = loggerFactory;
|
||||||
Logger = loggerFactory.CreateLogger(classType);
|
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