This repository has been archived on 2023-02-02. You can view files and clone it, but cannot push or open issues or pull requests.
mfgames-testsetup-cil/src/MfGames.TestSetup/TestBase.cs

47 lines
1.3 KiB
C#
Raw Normal View History

2021-09-10 17:12:49 +00:00
using Serilog;
using Serilog.Core;
using Serilog.Events;
using Serilog.Exceptions;
using Xunit.Abstractions;
namespace MfGames.TestSetup
{
/// <summary>
/// Sets up a test that uses Serilog for logging, Autofac as container
/// resolution, and other common configuration elements.
/// </summary>
public abstract class TestBase<TContext>
where TContext : TestContext, new()
{
protected TestBase(ITestOutputHelper output)
{
// Set up the logging output.
this.Output = output;
this.Logger = new LoggerConfiguration()
.Enrich.WithDemystifiedStackTraces()
.Enrich.WithExceptionDetails()
.Enrich.FromLogContext()
.MinimumLevel.Is(LogEventLevel.Debug)
.WriteTo.Console(
outputTemplate:
"[{Level:u3}] {Message}{NewLine}{Exception}")
.CreateLogger();
}
protected Logger Logger { get; }
protected ITestOutputHelper Output { get; }
protected virtual TContext CreateContext()
{
var context = new TContext();
context.SetLogger(this.Logger);
context.ConfigureContainer();
return context;
}
}
}