141 lines
4.4 KiB
C#
Executable File
141 lines
4.4 KiB
C#
Executable File
using Microsoft.AspNetCore;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Hosting.Server.Features;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Opsoft.NetCore.O3App.Infrastructure;
|
|
using Opsoft.NetCore.O3App.Shared;
|
|
using Serilog;
|
|
using Serilog.Events;
|
|
using System;
|
|
using System.Linq;
|
|
using System.Net.NetworkInformation;
|
|
|
|
namespace Opsoft.NetCore.O3App.Test
|
|
{
|
|
public class TestStartupFixture : IDisposable
|
|
{
|
|
private readonly IWebHost server;
|
|
public IServiceProvider Services { get; }
|
|
|
|
public string ServiceBaseUrl { get; }
|
|
|
|
private System.Collections.Generic.Dictionary<string, string> defaultConfigs = new System.Collections.Generic.Dictionary<string, string>();
|
|
|
|
public TestStartupFixture()
|
|
{
|
|
SetDefaultConfigs();
|
|
|
|
var builder = WebHost.CreateDefaultBuilder()
|
|
.UseUrls($"http://localhost:{GetRandomPort()}")
|
|
.ConfigureAppConfiguration(ConfigureAppConfiguration)
|
|
.ConfigureServices(ServiceRegister.RegisteServices)
|
|
.UseSerilog(UseSkynetLogger)
|
|
.UseKestrel()
|
|
.UseStartup<TestStartup>();
|
|
|
|
server = builder.Build();
|
|
server.Start();
|
|
|
|
var url = server.ServerFeatures.Get<IServerAddressesFeature>().Addresses.First();
|
|
Services = server.Services;
|
|
ServiceBaseUrl = $"{url}/api/";
|
|
|
|
Initialize();
|
|
}
|
|
|
|
private void ConfigureAppConfiguration(WebHostBuilderContext context, IConfigurationBuilder builder)
|
|
{
|
|
builder.SetBasePath(System.IO.Directory.GetCurrentDirectory());
|
|
builder
|
|
.AddInMemoryCollection(defaultConfigs)
|
|
.AddEnvironmentVariables()
|
|
.AddJsonFile("appsetting.json", optional: true, reloadOnChange: true);
|
|
var testConfigFiles = System.IO.Directory.EnumerateFiles(".\\TestDatas", "*.json", System.IO.SearchOption.TopDirectoryOnly);
|
|
if (testConfigFiles.NotNullOrEmpty())
|
|
{
|
|
foreach (var configFile in testConfigFiles)
|
|
{
|
|
builder.AddJsonFile(configFile, optional: false, reloadOnChange: true);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// TestDataInitialize
|
|
/// </summary>
|
|
private void Initialize()
|
|
{
|
|
// ...
|
|
}
|
|
|
|
private void SetDefaultConfigs()
|
|
{
|
|
defaultConfigs.Add(nameof(LoggerFilters), LoggerFilters);
|
|
|
|
defaultConfigs.Add("DAOKEAPPUK", "opsoft.netcore.O3App");
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
server.Dispose();
|
|
}
|
|
|
|
private static readonly Random Random = new Random();
|
|
|
|
private static int GetRandomPort()
|
|
{
|
|
var activePorts = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners().Select(_ => _.Port).ToList();
|
|
|
|
var randomPort = Random.Next(10000, 65535);
|
|
|
|
while (activePorts.Contains(randomPort))
|
|
{
|
|
randomPort = Random.Next(10000, 65535);
|
|
}
|
|
|
|
return randomPort;
|
|
}
|
|
|
|
|
|
private static void UseSkynetLogger(WebHostBuilderContext context, LoggerConfiguration configuration)
|
|
{
|
|
string logPath = $"/data/logs/app.log";
|
|
long? fileSizeLimitBytes = 10485760;
|
|
bool buffered = false;
|
|
bool shared = true;
|
|
TimeSpan? flushToDiskInterval = null;
|
|
bool rollOnFileSizeLimit = true;
|
|
int? retainedFileCountLimit = 640;
|
|
configuration.Filter.With(new SkynetLoggerFilter(context.Configuration));
|
|
configuration.Enrich.With(new SkynetEnricher())
|
|
.WriteTo.File(new SkynetLoggerFormatter(), logPath, LogEventLevel.Debug,
|
|
fileSizeLimitBytes, null, buffered, shared,
|
|
flushToDiskInterval, RollingInterval.Day, rollOnFileSizeLimit,
|
|
retainedFileCountLimit);
|
|
}
|
|
|
|
private static readonly string LoggerFilters = @"[
|
|
{
|
|
""Catogery"": ""System"",
|
|
""MinLevel"": 5
|
|
},
|
|
{
|
|
""Catogery"": ""Microsoft"",
|
|
""MinLevel"": 5
|
|
},
|
|
{
|
|
""Catogery"": ""Opsoft.Apm"",
|
|
""MinLevel"": 5
|
|
},
|
|
{
|
|
""Catogery"": ""Opsoft.NetCore.O3App.Services"",
|
|
""MinLevel"": 1
|
|
},
|
|
{
|
|
""Catogery"": ""Opsoft.NetCore.O3App.Test"",
|
|
""MinLevel"": 4
|
|
}
|
|
]";
|
|
|
|
}
|
|
} |