Skip to content

Commit

Permalink
Merge pull request #20 from LykkeCity/Logging-system-redesign
Browse files Browse the repository at this point in the history
Logging system redesign. Backward compatibility restored
  • Loading branch information
KonstantinRyazantsev committed Jun 25, 2018
2 parents dc84390 + 0a8efdb commit f02351d
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ public class ClientErrorHandlerMiddleware
private readonly ILog _log;
private readonly RequestDelegate _next;

[Obsolete]
public ClientErrorHandlerMiddleware(RequestDelegate next, ILog log, string componentName)
{
if (log == null)
{
throw new ArgumentNullException(nameof(log));
}

_log = log.CreateComponentScope(componentName);
_next = next;
}

/// <summary>
/// Logs url, request body and response status for responses with status code = 4xx
/// </summary>
Expand Down Expand Up @@ -48,12 +60,13 @@ private async Task LogError(HttpContext context)
var urlWithoutQuery = RequestUtils.GetUrlWithoutQuery(url) ?? "?";
var body = await RequestUtils.GetRequestPartialBodyAsync(context);

_log.Warning(urlWithoutQuery, message: null, context: new
{
url = url,
statusCode = context.Response.StatusCode,
body = body
});
_log.WriteWarning(urlWithoutQuery, new
{
url = url,
statusCode = context.Response.StatusCode,
body = body
},
"");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ public class GlobalErrorHandlerMiddleware
private readonly CreateErrorResponse _createErrorResponse;
private readonly RequestDelegate _next;

[Obsolete]
public GlobalErrorHandlerMiddleware(RequestDelegate next, ILog log, string componentName, CreateErrorResponse createErrorResponse)
{
if (log == null)
{
throw new ArgumentNullException(nameof(log));
}

_log = log.CreateComponentScope(componentName);
_createErrorResponse = createErrorResponse ?? throw new ArgumentNullException(nameof(createErrorResponse));
_next = next;
}

/// <summary>
/// Middleware that handles all unhandled exceptions and use delegate to generate error response
/// </summary>
Expand Down Expand Up @@ -53,11 +66,12 @@ private async Task LogError(HttpContext context, Exception ex)
var urlWithoutQuery = RequestUtils.GetUrlWithoutQuery(url) ?? "?";
var body = await RequestUtils.GetRequestPartialBodyAsync(context);

_log.Error(urlWithoutQuery, ex, context: new
{
url = url,
body = body
});
_log.WriteError(urlWithoutQuery, new
{
url = url,
body = body
},
ex);
}

private async Task CreateErrorResponse(HttpContext ctx, Exception ex)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,54 @@
using System;
using Common.Log;
using JetBrains.Annotations;
using Lykke.Common.Log;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http.Internal;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.DependencyInjection;

namespace Lykke.Common.ApiLibrary.Middleware
{
[PublicAPI]
public static class MiddlewareApplicationBuilderExtensions
{
/// <summary>
/// Configure application to use standart Lykke middleware
/// </summary>
/// <param name="app">Application builder</param>
/// <param name="componentName">Component name for logs</param>
/// <param name="createGlobalErrorResponse">Create global error response delegate</param>
/// <param name="logClientErrors">log 4xx errors</param>
[Obsolete]
public static void UseLykkeMiddleware(
this IApplicationBuilder app,
string componentName,
CreateErrorResponse createGlobalErrorResponse,
bool logClientErrors = false)
{
app.Use(async (context, next) =>
{
// enable ability to seek on request stream within any host,
// but not only Kestrel, for any subsequent middleware
context.Request.EnableRewind();
await next();
});

var log = app.ApplicationServices.GetRequiredService<ILog>();

app.UseMiddleware<GlobalErrorHandlerMiddleware>(
log,
componentName,
createGlobalErrorResponse);

if (logClientErrors)
{
app.UseMiddleware<ClientErrorHandlerMiddleware>(
log,
componentName);
}
}

/// <summary>
/// Configure application to use standart Lykke middleware
/// </summary>
Expand Down Expand Up @@ -37,11 +77,15 @@ public static void UseLykkeMiddleware(
await next();
});

app.UseMiddleware<GlobalErrorHandlerMiddleware>(createGlobalErrorResponse);
var logFactory = app.ApplicationServices.GetRequiredService<ILogFactory>();

app.UseMiddleware<GlobalErrorHandlerMiddleware>(
logFactory,
createGlobalErrorResponse);

if (logClientErrors)
{
app.UseMiddleware<ClientErrorHandlerMiddleware>();
app.UseMiddleware<ClientErrorHandlerMiddleware>(logFactory);
}
}

Expand Down

0 comments on commit f02351d

Please sign in to comment.