-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Using Xunit test output helper in some tests #1362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
dc7dd83
b6affe6
efa7ae1
57e67e0
36f9f98
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -7,6 +7,7 @@ | |||
using Microsoft.Extensions.Logging; | ||||
using WorkflowCore.Interface; | ||||
using WorkflowCore.Models; | ||||
using Xunit.Abstractions; | ||||
|
||||
namespace WorkflowCore.Testing | ||||
{ | ||||
|
@@ -18,11 +19,16 @@ public abstract class WorkflowTest<TWorkflow, TData> : IDisposable | |||
protected IPersistenceProvider PersistenceProvider; | ||||
protected List<StepError> UnhandledStepErrors = new List<StepError>(); | ||||
|
||||
protected virtual void Setup() | ||||
protected virtual void Setup(ITestOutputHelper testOutputHelper = null) | ||||
{ | ||||
//setup dependency injection | ||||
IServiceCollection services = new ServiceCollection(); | ||||
services.AddLogging(); | ||||
services.AddLogging(l => l.SetMinimumLevel(LogLevel.Trace)); | ||||
services.AddSingleton<ILoggerProvider>(p => new XUnitLoggerProvider(testOutputHelper)); | ||||
services.Add(ServiceDescriptor.Singleton<ILoggerFactory, LoggerFactory>()); | ||||
services.Add(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(XUnitLogger<>))); | ||||
|
services.Add(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(XUnitLogger<>))); |
Copilot uses AI. Check for mistakes.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,91 @@ | ||||||
using System; | ||||||
using System.Text; | ||||||
using Microsoft.Extensions.Logging; | ||||||
using Xunit.Abstractions; | ||||||
|
||||||
namespace WorkflowCore.Testing | ||||||
{ | ||||||
internal class XUnitLogger : ILogger | ||||||
{ | ||||||
private readonly ITestOutputHelper _testOutputHelper; | ||||||
private readonly string _categoryName; | ||||||
private readonly LoggerExternalScopeProvider _scopeProvider; | ||||||
|
||||||
public static ILogger CreateLogger(ITestOutputHelper testOutputHelper) => | ||||||
new XUnitLogger(testOutputHelper, new LoggerExternalScopeProvider(), ""); | ||||||
|
||||||
public static ILogger<T> CreateLogger<T>(ITestOutputHelper testOutputHelper) => | ||||||
new XUnitLogger<T>(testOutputHelper, new LoggerExternalScopeProvider()); | ||||||
|
||||||
public XUnitLogger(ITestOutputHelper testOutputHelper, LoggerExternalScopeProvider scopeProvider, | ||||||
string categoryName) | ||||||
{ | ||||||
_testOutputHelper = testOutputHelper; | ||||||
_scopeProvider = scopeProvider; | ||||||
_categoryName = categoryName; | ||||||
} | ||||||
|
||||||
public bool IsEnabled(LogLevel logLevel) => logLevel != LogLevel.None; | ||||||
|
||||||
public IDisposable BeginScope<TState>(TState state) => _scopeProvider.Push(state); | ||||||
|
||||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, | ||||||
Func<TState, Exception, string> formatter) | ||||||
{ | ||||||
if (_testOutputHelper == null) return; | ||||||
var sb = new StringBuilder(); | ||||||
sb.Append(DateTime.Now.ToString("HH:mm:ss.fff")) | ||||||
.Append(" ") | ||||||
.Append(GetLogLevelString(logLevel)) | ||||||
.Append(" [").Append(_categoryName).Append("] ") | ||||||
.Append(formatter(state, exception)); | ||||||
|
||||||
if (exception != null) | ||||||
{ | ||||||
sb.Append('\n').Append(exception); | ||||||
} | ||||||
|
||||||
// Append scopes | ||||||
_scopeProvider.ForEachScope((scope, s) => | ||||||
{ | ||||||
s.Append("\n => "); | ||||||
s.Append(scope); | ||||||
}, sb); | ||||||
|
||||||
_testOutputHelper.WriteLine(sb.ToString()); | ||||||
} | ||||||
|
||||||
private static string GetLogLevelString(LogLevel logLevel) | ||||||
{ | ||||||
return logLevel.ToString().ToUpper(); | ||||||
|
return logLevel.ToString().ToUpper(); | |
return logLevel.ToString().ToUpper(); |
Copilot uses AI. Check for mistakes.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using WorkflowCore.IntegrationTests.Scenarios; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace WorkflowCore.Tests.Sqlite.Scenarios | ||
{ | ||
[Collection("Sqlite collection")] | ||
public class SqliteDelayScenario : DelayScenario | ||
{ | ||
public SqliteDelayScenario(ITestOutputHelper testOutputHelper) : base(testOutputHelper) | ||
{ | ||
} | ||
|
||
protected override void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddWorkflow(cfg => | ||
{ | ||
cfg.UseSqlite($"Data Source=wfc-tests-{DateTime.Now.Ticks}.db;", true); | ||
cfg.UsePollInterval(TimeSpan.FromSeconds(2)); | ||
}); | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Explicitly registering LoggerFactory as singleton may conflict with the existing logging setup from AddLogging(). The AddLogging() method already registers ILoggerFactory, so this additional registration could cause issues.
Copilot uses AI. Check for mistakes.