105 lines
No EOL
3.3 KiB
C#
105 lines
No EOL
3.3 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using Autofac;
|
|
using Core.Configurations;
|
|
using Core.ModuleRegistry;
|
|
using Microsoft.ApplicationInsights;
|
|
using Microsoft.Extensions.Logging;
|
|
using Core.Configurations.JsonConfigProvider;
|
|
using Serilog;
|
|
using Serilog.Extensions.Logging;
|
|
namespace Tests
|
|
{
|
|
/// <summary>
|
|
/// Act as base class for tests. Avoids duplication of test setup code
|
|
/// </summary>
|
|
[TestClass]
|
|
public abstract partial class TestFixture
|
|
{
|
|
protected IContainer Container { get; private set; }
|
|
protected ContainerBuilder ContainerBuilder { get; private set; }
|
|
|
|
public virtual IConfigurationRoot Configuration()
|
|
{
|
|
var configuration = new ConfigurationBuilder()
|
|
.AddJsonFile("appconfiguration.dev.json")
|
|
.Build();
|
|
|
|
return configuration;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Should not be overriden. Rather override PreArrangeAll to setup data needed for a test class.
|
|
/// Override PrebuildContainer with a method that does nothing to prevent early build of IOC container
|
|
/// </summary>
|
|
[TestInitialize]
|
|
public void Setup()
|
|
{
|
|
CreateContainerBuilder();
|
|
Container = ContainerBuilder.Build();
|
|
|
|
}
|
|
|
|
|
|
protected virtual void CreateContainerBuilder()
|
|
{
|
|
IConfigurationRoot configuration = Configuration();
|
|
|
|
//var logger = new LoggerConfiguration()
|
|
// .MinimumLevel.Verbose()
|
|
// .MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning)
|
|
// .MinimumLevel.Override("System", Serilog.Events.LogEventLevel.Error)
|
|
// .WriteTo.Seq("http://localhost:5341", apiKey: "Gt8hS9ClGNfOCAdswDlW")
|
|
// .WriteTo.ApplicationInsights(configuration.Get<string>("ApplicationInsights:ConnectionString"),
|
|
// TelemetryConverter.Traces)
|
|
// .Enrich.FromLogContext()
|
|
// .Enrich.WithMachineName()
|
|
// .CreateLogger();
|
|
|
|
//Log.Logger = logger;
|
|
|
|
//Log.Logger.Verbose("Is thos work");
|
|
var builder = new ContainerBuilder();
|
|
|
|
|
|
//builder.Register(c => new SerilogLoggerFactory(logger))
|
|
// .As<ILoggerFactory>()
|
|
// .SingleInstance();
|
|
|
|
builder.RegisterGeneric(typeof(Logger<>))
|
|
.As(typeof(ILogger<>))
|
|
.SingleInstance();
|
|
|
|
|
|
builder.RegisterModule(new Core.ModuleRegistry.DbPostgreSqlModule
|
|
{
|
|
ConnectionString = configuration.GetConnectionString("DefaultConnection")
|
|
});
|
|
|
|
builder.RegisterModule(new Core.ModuleRegistry.TelemetryModule
|
|
{
|
|
TelemetryConfig = configuration.GetSection("ApplicationInsights").ToObject<Core.ModuleRegistry.TelemetryConfig>()
|
|
});
|
|
|
|
|
|
ContainerBuilder = builder;
|
|
}
|
|
|
|
[TestCleanup]
|
|
public void CleanUp()
|
|
{
|
|
//Serilog.Log.CloseAndFlush();
|
|
|
|
Trace.Flush();
|
|
var telemetryClient = Container.Resolve<TelemetryClient>();
|
|
telemetryClient.Flush();
|
|
|
|
if (Container != null)
|
|
{
|
|
Container.Dispose();
|
|
Container = null;
|
|
}
|
|
}
|
|
|
|
}
|
|
} |