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