From 1cd3a9611c96dbee00fbb00625aed3dfee5798c3 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Tue, 5 Nov 2019 12:43:44 +0200 Subject: [PATCH] NDB.Logging.Api -> RequestLoggingConfiguration --- NDB.Logging.Api/DependencyInjectionExtensions.cs | 8 ++++++++ NDB.Logging.Api/Entities/RequestLoggingConfiguration.cs | 7 +++++++ NDB.Logging.Api/RequestLoggingAttribute.cs | 9 ++++++--- 3 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 NDB.Logging.Api/Entities/RequestLoggingConfiguration.cs diff --git a/NDB.Logging.Api/DependencyInjectionExtensions.cs b/NDB.Logging.Api/DependencyInjectionExtensions.cs index c86de68..9889d11 100644 --- a/NDB.Logging.Api/DependencyInjectionExtensions.cs +++ b/NDB.Logging.Api/DependencyInjectionExtensions.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.DependencyInjection; +using NDB.Logging.Api.Entities; namespace NDB.Logging.Api { @@ -6,6 +7,13 @@ namespace NDB.Logging.Api { public static void AddRequestLogging(this IServiceCollection services) { + services.AddSingleton(new RequestLoggingConfiguration() { LogBody = false }); + services.AddScoped(); + } + + public static void AddRequestLogging(this IServiceCollection services, bool logBody) + { + services.AddSingleton(new RequestLoggingConfiguration() { LogBody = logBody }); services.AddScoped(); } } diff --git a/NDB.Logging.Api/Entities/RequestLoggingConfiguration.cs b/NDB.Logging.Api/Entities/RequestLoggingConfiguration.cs new file mode 100644 index 0000000..21d90d2 --- /dev/null +++ b/NDB.Logging.Api/Entities/RequestLoggingConfiguration.cs @@ -0,0 +1,7 @@ +namespace NDB.Logging.Api.Entities +{ + public class RequestLoggingConfiguration + { + public bool LogBody { get; set; } + } +} diff --git a/NDB.Logging.Api/RequestLoggingAttribute.cs b/NDB.Logging.Api/RequestLoggingAttribute.cs index c0017a4..7b1224e 100644 --- a/NDB.Logging.Api/RequestLoggingAttribute.cs +++ b/NDB.Logging.Api/RequestLoggingAttribute.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Logging; +using NDB.Logging.Api.Entities; using System; using System.Diagnostics; using System.IO; @@ -13,10 +14,12 @@ namespace NDB.Logging.Api public class RequestLoggingAttribute : ActionFilterAttribute { private readonly ILogger _logger; + private readonly RequestLoggingConfiguration _requestLoggingConfiguration; - public RequestLoggingAttribute(ILogger logger) + public RequestLoggingAttribute(ILogger logger, RequestLoggingConfiguration requestLoggingConfiguration) { _logger = logger; + _requestLoggingConfiguration = requestLoggingConfiguration; } public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) @@ -33,12 +36,12 @@ namespace NDB.Logging.Api var logContent = $"Start request {uid} for method {requestUrl}"; - if (request.Method == WebRequestMethods.Http.Post) + if (_requestLoggingConfiguration.LogBody && request.Method == WebRequestMethods.Http.Post) { var requestBody = await GetRequestBody(request); logContent += $" with body {requestBody}"; } - + _logger.LogInformation(logContent); var resultContext = await next();