mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-27 10:47:01 +02:00
Add log4net logging (#89)
* logging + fixed proxy isses * Add more logging * comments * 1.0.3.1
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using log4net;
|
||||
using Newtonsoft.Json;
|
||||
#if !NETSTANDARD
|
||||
using Microsoft.Owin;
|
||||
@@ -15,6 +16,7 @@ namespace WireMock.Owin
|
||||
internal class GlobalExceptionMiddleware
|
||||
#endif
|
||||
{
|
||||
private static readonly ILog Log = LogManager.GetLogger(typeof(GlobalExceptionMiddleware));
|
||||
#if !NETSTANDARD
|
||||
public GlobalExceptionMiddleware(OwinMiddleware next) : base(next) { }
|
||||
#else
|
||||
@@ -42,6 +44,7 @@ namespace WireMock.Owin
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error("HttpStatusCode set to 500", ex);
|
||||
await _responseMapper.MapAsync(new ResponseMessage { StatusCode = 500, Body = JsonConvert.SerializeObject(ex) }, ctx.Response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WireMock.Util;
|
||||
#if !NETSTANDARD
|
||||
using Microsoft.Owin;
|
||||
#else
|
||||
@@ -16,7 +17,7 @@ namespace WireMock.Owin
|
||||
/// <summary>
|
||||
/// OwinRequestMapper
|
||||
/// </summary>
|
||||
public class OwinRequestMapper
|
||||
internal class OwinRequestMapper
|
||||
{
|
||||
/// <summary>
|
||||
/// MapAsync IOwinRequest to RequestMessage
|
||||
@@ -43,20 +44,6 @@ namespace WireMock.Owin
|
||||
#endif
|
||||
string method = request.Method;
|
||||
|
||||
string bodyAsString = null;
|
||||
byte[] body = null;
|
||||
Encoding bodyEncoding = null;
|
||||
if (ParseBody(method) && request.Body != null)
|
||||
{
|
||||
using (var streamReader = new StreamReader(request.Body))
|
||||
{
|
||||
bodyAsString = await streamReader.ReadToEndAsync();
|
||||
bodyEncoding = streamReader.CurrentEncoding;
|
||||
}
|
||||
|
||||
body = bodyEncoding.GetBytes(bodyAsString);
|
||||
}
|
||||
|
||||
Dictionary<string, string[]> headers = null;
|
||||
if (request.Headers.Any())
|
||||
{
|
||||
@@ -77,10 +64,16 @@ namespace WireMock.Owin
|
||||
}
|
||||
}
|
||||
|
||||
return new RequestMessage(url, method, clientIP, body, bodyAsString, bodyEncoding, headers, cookies) { DateTime = DateTime.Now };
|
||||
BodyData body = null;
|
||||
if (request.Body != null && ShouldParseBody(method))
|
||||
{
|
||||
body = await BodyParser.Parse(request.Body, request.ContentType);
|
||||
}
|
||||
|
||||
return new RequestMessage(url, method, clientIP, body, headers, cookies) { DateTime = DateTime.Now };
|
||||
}
|
||||
|
||||
private bool ParseBody(string method)
|
||||
private bool ShouldParseBody(string method)
|
||||
{
|
||||
/*
|
||||
HEAD - No defined body semantics.
|
||||
|
||||
@@ -3,9 +3,11 @@ using System.Threading.Tasks;
|
||||
using WireMock.Logging;
|
||||
using WireMock.Matchers.Request;
|
||||
using System.Linq;
|
||||
using log4net;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Util;
|
||||
using Newtonsoft.Json;
|
||||
using WireMock.Http;
|
||||
#if !NETSTANDARD
|
||||
using Microsoft.Owin;
|
||||
#else
|
||||
@@ -20,6 +22,7 @@ namespace WireMock.Owin
|
||||
internal class WireMockMiddleware
|
||||
#endif
|
||||
{
|
||||
private static readonly ILog Log = LogManager.GetLogger(typeof(WireMockMiddleware));
|
||||
private static readonly Task CompletedTask = Task.FromResult(false);
|
||||
private readonly WireMockMiddlewareOptions _options;
|
||||
|
||||
@@ -95,6 +98,7 @@ namespace WireMock.Owin
|
||||
if (targetMapping == null)
|
||||
{
|
||||
logRequest = true;
|
||||
Log.Warn("HttpStatusCode set to 404 : No matching mapping found");
|
||||
response = new ResponseMessage { StatusCode = 404, Body = "No matching mapping found" };
|
||||
return;
|
||||
}
|
||||
@@ -103,9 +107,10 @@ namespace WireMock.Owin
|
||||
|
||||
if (targetMapping.IsAdminInterface && _options.AuthorizationMatcher != null)
|
||||
{
|
||||
bool present = request.Headers.TryGetValue("Authorization", out WireMockList<string> authorization);
|
||||
bool present = request.Headers.TryGetValue(HttpKnownHeaderNames.Authorization, out WireMockList<string> authorization);
|
||||
if (!present || _options.AuthorizationMatcher.IsMatch(authorization.ToString()) < MatchScores.Perfect)
|
||||
{
|
||||
Log.Error("HttpStatusCode set to 401");
|
||||
response = new ResponseMessage { StatusCode = 401 };
|
||||
return;
|
||||
}
|
||||
@@ -125,6 +130,7 @@ namespace WireMock.Owin
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error("HttpStatusCode set to 500", ex);
|
||||
response = new ResponseMessage { StatusCode = 500, Body = JsonConvert.SerializeObject(ex) };
|
||||
}
|
||||
finally
|
||||
|
||||
Reference in New Issue
Block a user