mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-23 01:08:49 +02:00
Issue 225 - Improve logging in example for WireMock as Windows Service (#247)
This commit is contained in:
committed by
Stef Heyenrath
parent
33f40c0e84
commit
62823688a3
@@ -1,6 +1,8 @@
|
|||||||
using System;
|
using log4net.Config;
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
using System.ServiceProcess;
|
using System.ServiceProcess;
|
||||||
using WireMock.Logging;
|
using WireMock.Net.Service;
|
||||||
using WireMock.Net.StandAlone;
|
using WireMock.Net.StandAlone;
|
||||||
using WireMock.Server;
|
using WireMock.Server;
|
||||||
using WireMock.Settings;
|
using WireMock.Settings;
|
||||||
@@ -21,7 +23,7 @@ namespace Wiremock.Net.Service
|
|||||||
|
|
||||||
protected override void OnStart(string[] args)
|
protected override void OnStart(string[] args)
|
||||||
{
|
{
|
||||||
Start(new WireMockNullLogger());
|
Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnStop()
|
protected override void OnStop()
|
||||||
@@ -35,6 +37,11 @@ namespace Wiremock.Net.Service
|
|||||||
|
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
|
//Setting the current directory explicitly is required if the application is running as Windows Service,
|
||||||
|
//as the current directory of a Windows Service is %WinDir%\System32 per default.
|
||||||
|
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
|
||||||
|
XmlConfigurator.ConfigureAndWatch(new FileInfo("log4net.config"));
|
||||||
|
|
||||||
// running as service
|
// running as service
|
||||||
if (!Environment.UserInteractive)
|
if (!Environment.UserInteractive)
|
||||||
{
|
{
|
||||||
@@ -46,7 +53,7 @@ namespace Wiremock.Net.Service
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// running as console app
|
// running as console app
|
||||||
Start(new WireMockConsoleLogger());
|
Start();
|
||||||
|
|
||||||
Console.WriteLine("Press any key to stop...");
|
Console.WriteLine("Press any key to stop...");
|
||||||
Console.ReadKey(true);
|
Console.ReadKey(true);
|
||||||
@@ -55,14 +62,14 @@ namespace Wiremock.Net.Service
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void Start(IWireMockLogger logger)
|
private static void Start()
|
||||||
{
|
{
|
||||||
_server = StandAloneApp.Start(new FluentMockServerSettings
|
_server = StandAloneApp.Start(new FluentMockServerSettings
|
||||||
{
|
{
|
||||||
Urls = new[] { "http://*:9091/" },
|
Urls = new[] { "http://*:9091/" },
|
||||||
StartAdminInterface = true,
|
StartAdminInterface = true,
|
||||||
ReadStaticMappings = true,
|
ReadStaticMappings = true,
|
||||||
Logger = logger
|
Logger = new WireMockLog4NetLogger()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,6 +35,9 @@
|
|||||||
<Reference Include="Handlebars, Version=1.9.5.0, Culture=neutral, PublicKeyToken=22225d0bf33cd661, processorArchitecture=MSIL">
|
<Reference Include="Handlebars, Version=1.9.5.0, Culture=neutral, PublicKeyToken=22225d0bf33cd661, processorArchitecture=MSIL">
|
||||||
<HintPath>..\..\packages\Handlebars.Net.1.9.5\lib\net452\Handlebars.dll</HintPath>
|
<HintPath>..\..\packages\Handlebars.Net.1.9.5\lib\net452\Handlebars.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
<Reference Include="log4net, Version=2.0.8.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
|
||||||
|
<HintPath>..\..\packages\log4net.2.0.8\lib\net45-full\log4net.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
<Reference Include="Microsoft.Owin, Version=2.0.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
<Reference Include="Microsoft.Owin, Version=2.0.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||||
<HintPath>..\..\packages\Microsoft.Owin.2.0.2\lib\net45\Microsoft.Owin.dll</HintPath>
|
<HintPath>..\..\packages\Microsoft.Owin.2.0.2\lib\net45\Microsoft.Owin.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
@@ -96,9 +99,13 @@
|
|||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="Program.cs" />
|
<Compile Include="Program.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
<Compile Include="WireMockLog4NetLogger.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="App.config" />
|
<None Include="App.config" />
|
||||||
|
<None Include="log4net.config">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
<None Include="Service-Start.bat">
|
<None Include="Service-Start.bat">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
|
|||||||
39
examples/WireMock.Net.Service/WireMockLog4NetLogger.cs
Normal file
39
examples/WireMock.Net.Service/WireMockLog4NetLogger.cs
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
using log4net;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Wiremock.Net.Service;
|
||||||
|
using WireMock.Admin.Requests;
|
||||||
|
using WireMock.Logging;
|
||||||
|
|
||||||
|
namespace WireMock.Net.Service
|
||||||
|
{
|
||||||
|
internal class WireMockLog4NetLogger : IWireMockLogger
|
||||||
|
{
|
||||||
|
private static readonly ILog Log = LogManager.GetLogger(typeof(Program));
|
||||||
|
|
||||||
|
public void Debug(string formatString, params object[] args)
|
||||||
|
{
|
||||||
|
Log.DebugFormat(formatString, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Info(string formatString, params object[] args)
|
||||||
|
{
|
||||||
|
Log.InfoFormat(formatString, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Warn(string formatString, params object[] args)
|
||||||
|
{
|
||||||
|
Log.WarnFormat(formatString, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Error(string formatString, params object[] args)
|
||||||
|
{
|
||||||
|
Log.ErrorFormat(formatString, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DebugRequestResponse(LogEntryModel logEntryModel, bool isAdminRequest)
|
||||||
|
{
|
||||||
|
string message = JsonConvert.SerializeObject(logEntryModel, Formatting.Indented);
|
||||||
|
Log.DebugFormat("Admin[{0}] {1}", isAdminRequest, message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
38
examples/WireMock.Net.Service/log4net.config
Normal file
38
examples/WireMock.Net.Service/log4net.config
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8" ?>
|
||||||
|
<configuration>
|
||||||
|
<configSections>
|
||||||
|
<section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
|
||||||
|
</configSections>
|
||||||
|
<appSettings>
|
||||||
|
<add key="log4net.Internal.Debug" value="true"/>
|
||||||
|
</appSettings>
|
||||||
|
<log4net>
|
||||||
|
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
|
||||||
|
<layout type="log4net.Layout.PatternLayout">
|
||||||
|
<conversionPattern value="%date [%thread] %-5level %logger{1} - %message%newline" />
|
||||||
|
</layout>
|
||||||
|
</appender>
|
||||||
|
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
|
||||||
|
<file value="log.txt" />
|
||||||
|
<appendToFile value="true" />
|
||||||
|
<rollingStyle value="Size" />
|
||||||
|
<maxSizeRollBackups value="10" />
|
||||||
|
<maximumFileSize value="10000KB" />
|
||||||
|
<staticLogFileName value="true" />
|
||||||
|
<layout type="log4net.Layout.PatternLayout">
|
||||||
|
<conversionPattern value="%date [%thread] %-5level %logger{1} - %message%newline" />
|
||||||
|
</layout>
|
||||||
|
</appender>
|
||||||
|
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
|
||||||
|
<layout type="log4net.Layout.PatternLayout">
|
||||||
|
<conversionPattern value="%date [%thread] %-5level %logger{1} - %message%newline" />
|
||||||
|
</layout>
|
||||||
|
</appender>
|
||||||
|
<root>
|
||||||
|
<level value="DEBUG" />
|
||||||
|
<appender-ref ref="ConsoleAppender" />
|
||||||
|
<appender-ref ref="RollingLogFileAppender" />
|
||||||
|
<!--<appender-ref ref="EventLogAppender" /> --> <!-- comment in if needed -->
|
||||||
|
</root>
|
||||||
|
</log4net>
|
||||||
|
</configuration>
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<packages>
|
<packages>
|
||||||
<package id="Handlebars.Net" version="1.9.5" targetFramework="net452" />
|
<package id="Handlebars.Net" version="1.9.5" targetFramework="net452" />
|
||||||
|
<package id="log4net" version="2.0.8" targetFramework="net452" />
|
||||||
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
|
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
|
||||||
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />
|
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />
|
||||||
<package id="Microsoft.AspNet.WebApi.Owin" version="5.2.3" targetFramework="net452" />
|
<package id="Microsoft.AspNet.WebApi.Owin" version="5.2.3" targetFramework="net452" />
|
||||||
|
|||||||
Reference in New Issue
Block a user