Changed log messages to be a bit more descriptive

This commit is contained in:
Marc Denman 2017-03-13 17:23:09 +00:00
parent 0ec7fc44ad
commit 3fb83077fb
2 changed files with 75 additions and 72 deletions

View File

@ -6,6 +6,9 @@ using Ocelot.Logging;
namespace Ocelot.Errors.Middleware
{
/// <summary>
/// Catches all unhandled exceptions thrown by middleware, logs and returns a 500
/// </summary>
public class ExceptionHandlerMiddleware
{
private readonly RequestDelegate _next;

View File

@ -1,73 +1,73 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Ocelot.Errors;
using Ocelot.Infrastructure.RequestData;
using Ocelot.Logging;
using Ocelot.Middleware;
namespace Ocelot.Responder.Middleware
{
public class ResponderMiddleware : OcelotMiddleware
{
private readonly RequestDelegate _next;
private readonly IHttpResponder _responder;
private readonly IErrorsToHttpStatusCodeMapper _codeMapper;
private readonly IOcelotLogger _logger;
public ResponderMiddleware(RequestDelegate next,
IHttpResponder responder,
IOcelotLoggerFactory loggerFactory,
IRequestScopedDataRepository requestScopedDataRepository,
IErrorsToHttpStatusCodeMapper codeMapper)
:base(requestScopedDataRepository)
{
_next = next;
_responder = responder;
_codeMapper = codeMapper;
_logger = loggerFactory.CreateLogger<ResponderMiddleware>();
}
public async Task Invoke(HttpContext context)
{
_logger.LogDebug("started error responder middleware");
await _next.Invoke(context);
_logger.LogDebug("calling next middleware");
if (PipelineError)
{
_logger.LogDebug("there is a pipeline error, getting errors");
var errors = PipelineErrors;
_logger.LogDebug("received errors setting error response");
SetErrorResponse(context, errors);
}
else
{
_logger.LogDebug("no pipeline error, setting response");
await _responder.SetResponseOnHttpContext(context, HttpResponseMessage);
}
}
private void SetErrorResponse(HttpContext context, List<Error> errors)
{
var statusCode = _codeMapper.Map(errors);
if (!statusCode.IsError)
{
_responder.SetErrorResponseOnContext(context, statusCode.Data);
}
else
{
_responder.SetErrorResponseOnContext(context, 500);
}
}
}
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Ocelot.Errors;
using Ocelot.Infrastructure.RequestData;
using Ocelot.Logging;
using Ocelot.Middleware;
namespace Ocelot.Responder.Middleware
{
/// <summary>
/// Completes and returns the request and request body, if any pipeline errors occured then sets the appropriate HTTP status code instead.
/// </summary>
public class ResponderMiddleware : OcelotMiddleware
{
private readonly RequestDelegate _next;
private readonly IHttpResponder _responder;
private readonly IErrorsToHttpStatusCodeMapper _codeMapper;
private readonly IOcelotLogger _logger;
public ResponderMiddleware(RequestDelegate next,
IHttpResponder responder,
IOcelotLoggerFactory loggerFactory,
IRequestScopedDataRepository requestScopedDataRepository,
IErrorsToHttpStatusCodeMapper codeMapper)
:base(requestScopedDataRepository)
{
_next = next;
_responder = responder;
_codeMapper = codeMapper;
_logger = loggerFactory.CreateLogger<ResponderMiddleware>();
}
public async Task Invoke(HttpContext context)
{
_logger.LogDebug($"entered {this.GetType().Name}");
_logger.LogDebug($"invoking next middleware from {this.GetType().Name}");
await _next.Invoke(context);
_logger.LogDebug($"returned to {this.GetType().Name} after next middleware completed");
if (PipelineError)
{
var errors = PipelineErrors;
_logger.LogDebug($"{errors.Count} pipeline errors found in {this.GetType().Name}. Setting error response status code");
SetErrorResponse(context, errors);
}
else
{
_logger.LogDebug("no pipeline errors, setting and returning completed response");
await _responder.SetResponseOnHttpContext(context, HttpResponseMessage);
}
_logger.LogDebug($"completed {this.GetType().Name}");
}
private void SetErrorResponse(HttpContext context, List<Error> errors)
{
var statusCode = _codeMapper.Map(errors);
if (!statusCode.IsError)
{
_responder.SetErrorResponseOnContext(context, statusCode.Data);
}
else
{
_responder.SetErrorResponseOnContext(context, 500);
}
}
}
}