mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-17 14:40:00 +02:00
Apply ILogger, IOptions, IHostedService patterns to .NET Core 3.1 example (#514)
This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
-->
|
-->
|
||||||
<system.webServer>
|
<system.webServer>
|
||||||
<handlers>
|
<handlers>
|
||||||
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
|
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
|
||||||
</handlers>
|
</handlers>
|
||||||
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" />
|
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" />
|
||||||
</system.webServer>
|
</system.webServer>
|
||||||
|
|||||||
@@ -1,22 +1,28 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace WireMock.Net.WebApplication
|
namespace WireMock.Net.WebApplication
|
||||||
{
|
{
|
||||||
public class App
|
public class App : IHostedService
|
||||||
{
|
{
|
||||||
private readonly IWireMockService _service;
|
private readonly IWireMockService _service;
|
||||||
private readonly ILogger _logger;
|
|
||||||
|
|
||||||
public App(IWireMockService service, ILogger logger)
|
public App(IWireMockService service)
|
||||||
{
|
{
|
||||||
_service = service;
|
_service = service;
|
||||||
_logger = logger;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Run()
|
public Task StartAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("WireMock.Net App running");
|
_service.Start();
|
||||||
_service.Run();
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task StopAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
_service.Stop();
|
||||||
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,8 @@
|
|||||||
{
|
{
|
||||||
public interface IWireMockService
|
public interface IWireMockService
|
||||||
{
|
{
|
||||||
void Run();
|
void Start();
|
||||||
|
|
||||||
|
void Stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,59 +1,30 @@
|
|||||||
using System;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using WireMock.Settings;
|
using WireMock.Settings;
|
||||||
// using Microsoft.Extensions.Logging.Console;
|
|
||||||
|
namespace WireMock.Net.WebApplication
|
||||||
namespace WireMock.Net.WebApplication
|
{
|
||||||
{
|
public class Program
|
||||||
public class Program
|
{
|
||||||
{
|
public static void Main(string[] args)
|
||||||
public static void Main(string[] args)
|
{
|
||||||
{
|
CreateHostBuilder(args).Build().Run();
|
||||||
// Create service collection
|
}
|
||||||
var serviceCollection = new ServiceCollection();
|
|
||||||
ConfigureServices(serviceCollection);
|
private static IHostBuilder CreateHostBuilder(string[] args)
|
||||||
|
=> Host.CreateDefaultBuilder(args)
|
||||||
// Create service provider
|
.ConfigureServices((host, services) => ConfigureServices(services, host.Configuration));
|
||||||
var serviceProvider = serviceCollection.BuildServiceProvider();
|
|
||||||
|
private static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
|
||||||
// Run app
|
{
|
||||||
serviceProvider.GetService<App>().Run();
|
services.AddLogging(logging => logging.AddConsole().AddDebug());
|
||||||
}
|
|
||||||
|
services.AddTransient<IWireMockService, WireMockService>();
|
||||||
private static void ConfigureServices(IServiceCollection serviceCollection)
|
services.Configure<WireMockServerSettings>(configuration.GetSection("WireMockServerSettings"));
|
||||||
{
|
|
||||||
// Build configuration
|
services.AddHostedService<App>();
|
||||||
var configuration = new ConfigurationBuilder()
|
}
|
||||||
.SetBasePath(AppContext.BaseDirectory)
|
}
|
||||||
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
|
||||||
.AddEnvironmentVariables() // <-- this is needed to to override settings via the Azure Portal App Settings
|
|
||||||
.Build();
|
|
||||||
|
|
||||||
// Add LoggerFactory
|
|
||||||
var factory = new LoggerFactory();
|
|
||||||
//var consoleLog = new ConsoleLoggerProvider(configuration.GetSection("Logging"));
|
|
||||||
serviceCollection.AddSingleton(factory
|
|
||||||
// .AddProvider(configuration.GetSection("Logging"))
|
|
||||||
// .AddDebug()
|
|
||||||
// .AddAzureWebAppDiagnostics()
|
|
||||||
);
|
|
||||||
|
|
||||||
serviceCollection.AddSingleton(factory.CreateLogger("WireMock.Net Logger"));
|
|
||||||
|
|
||||||
// Add access to generic IConfigurationRoot
|
|
||||||
serviceCollection.AddSingleton(configuration);
|
|
||||||
|
|
||||||
// Add access to WireMockServerSettings
|
|
||||||
var settings = configuration.GetSection("WireMockServerSettings").Get<WireMockServerSettings>();
|
|
||||||
serviceCollection.AddSingleton<WireMockServerSettings>(settings);
|
|
||||||
|
|
||||||
// Add services
|
|
||||||
serviceCollection.AddTransient<IWireMockService, WireMockService>();
|
|
||||||
|
|
||||||
// Add app
|
|
||||||
serviceCollection.AddTransient<App>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
},
|
},
|
||||||
"iisExpress": {
|
"iisExpress": {
|
||||||
"applicationUrl": "http://localhost:56513/",
|
"applicationUrl": "http://localhost:56513/",
|
||||||
"sslPort": 0
|
"sslPort": 44304
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"profiles": {
|
"profiles": {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
<AssemblyName>WireMock.Net.WebApplication</AssemblyName>
|
<AssemblyName>WireMock.Net.WebApplication</AssemblyName>
|
||||||
<RootNamespace>WireMock.Net.WebApplication</RootNamespace>
|
<RootNamespace>WireMock.Net.WebApplication</RootNamespace>
|
||||||
<UserSecretsId>efcf4a18-fd7c-4622-1111-336d65290599</UserSecretsId>
|
<UserSecretsId>efcf4a18-fd7c-4622-1111-336d65290599</UserSecretsId>
|
||||||
|
<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -1,83 +1,82 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Threading;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Options;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using WireMock.Admin.Requests;
|
using WireMock.Admin.Requests;
|
||||||
using WireMock.Logging;
|
using WireMock.Logging;
|
||||||
using WireMock.Server;
|
using WireMock.Server;
|
||||||
using WireMock.Settings;
|
using WireMock.Settings;
|
||||||
|
|
||||||
namespace WireMock.Net.WebApplication
|
namespace WireMock.Net.WebApplication
|
||||||
{
|
{
|
||||||
public class WireMockService : IWireMockService
|
public class WireMockService : IWireMockService
|
||||||
{
|
{
|
||||||
private static int sleepTime = 30000;
|
private WireMockServer _server;
|
||||||
|
private readonly ILogger _logger;
|
||||||
private readonly ILogger _logger;
|
private readonly WireMockServerSettings _settings;
|
||||||
private readonly WireMockServerSettings _settings;
|
|
||||||
|
private class Logger : IWireMockLogger
|
||||||
private class Logger : IWireMockLogger
|
{
|
||||||
{
|
private readonly ILogger _logger;
|
||||||
private readonly ILogger _logger;
|
|
||||||
|
public Logger(ILogger logger)
|
||||||
public Logger(ILogger logger)
|
{
|
||||||
{
|
_logger = logger;
|
||||||
_logger = logger;
|
}
|
||||||
}
|
|
||||||
|
public void Debug(string formatString, params object[] args)
|
||||||
public void Debug(string formatString, params object[] args)
|
{
|
||||||
{
|
_logger.LogDebug(formatString, args);
|
||||||
_logger.LogDebug(formatString, args);
|
}
|
||||||
}
|
|
||||||
|
public void Info(string formatString, params object[] args)
|
||||||
public void Info(string formatString, params object[] args)
|
{
|
||||||
{
|
_logger.LogInformation(formatString, args);
|
||||||
_logger.LogInformation(formatString, args);
|
}
|
||||||
}
|
|
||||||
|
public void Warn(string formatString, params object[] args)
|
||||||
public void Warn(string formatString, params object[] args)
|
{
|
||||||
{
|
_logger.LogWarning(formatString, args);
|
||||||
_logger.LogWarning(formatString, args);
|
}
|
||||||
}
|
|
||||||
|
public void Error(string formatString, params object[] args)
|
||||||
public void Error(string formatString, params object[] args)
|
{
|
||||||
{
|
_logger.LogError(formatString, args);
|
||||||
_logger.LogError(formatString, args);
|
}
|
||||||
}
|
|
||||||
|
public void DebugRequestResponse(LogEntryModel logEntryModel, bool isAdminrequest)
|
||||||
public void DebugRequestResponse(LogEntryModel logEntryModel, bool isAdminrequest)
|
{
|
||||||
{
|
string message = JsonConvert.SerializeObject(logEntryModel, Formatting.Indented);
|
||||||
string message = JsonConvert.SerializeObject(logEntryModel, Formatting.Indented);
|
_logger.LogDebug("Admin[{0}] {1}", isAdminrequest, message);
|
||||||
_logger.LogDebug("Admin[{0}] {1}", isAdminrequest, message);
|
}
|
||||||
}
|
|
||||||
|
public void Error(string formatString, Exception exception)
|
||||||
public void Error(string formatString, Exception exception)
|
{
|
||||||
{
|
_logger.LogError(formatString, exception.Message);
|
||||||
_logger.LogError(formatString, exception.Message);
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public WireMockService(ILogger<WireMockService> logger, IOptions<WireMockServerSettings> settings)
|
||||||
public WireMockService(ILogger logger, WireMockServerSettings settings)
|
{
|
||||||
{
|
_logger = logger;
|
||||||
_logger = logger;
|
_settings = settings.Value;
|
||||||
_settings = settings;
|
|
||||||
|
_settings.Logger = new Logger(logger);
|
||||||
_settings.Logger = new Logger(logger);
|
}
|
||||||
}
|
|
||||||
|
public void Start()
|
||||||
public void Run()
|
{
|
||||||
{
|
_logger.LogInformation("WireMock.Net server starting");
|
||||||
_logger.LogInformation("WireMock.Net server starting");
|
|
||||||
|
_server = WireMockServer.Start(_settings);
|
||||||
WireMockServer.Start(_settings);
|
|
||||||
|
_logger.LogInformation($"WireMock.Net server settings {JsonConvert.SerializeObject(_settings)}");
|
||||||
_logger.LogInformation($"WireMock.Net server settings {JsonConvert.SerializeObject(_settings)}");
|
}
|
||||||
|
|
||||||
while (true)
|
public void Stop()
|
||||||
{
|
{
|
||||||
_logger.LogInformation("WireMock.Net server running");
|
_logger.LogInformation("WireMock.Net server stopping");
|
||||||
Thread.Sleep(sleepTime);
|
_server?.Stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -7,6 +7,6 @@
|
|||||||
<handlers>
|
<handlers>
|
||||||
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
|
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
|
||||||
</handlers>
|
</handlers>
|
||||||
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" hostingModel="InProcess" />
|
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" />
|
||||||
</system.webServer>
|
</system.webServer>
|
||||||
</configuration>
|
</configuration>
|
||||||
Reference in New Issue
Block a user