From d5890728adac21cce30d408a5339d5ea557626b4 Mon Sep 17 00:00:00 2001 From: yurekliisa Date: Fri, 22 Jan 2021 18:12:19 +0300 Subject: [PATCH 1/4] added log pipeline --- .../appsettings.json | 2 +- .../Extensions/ServiceCollectionExtensions.cs | 5 ++- .../Pipelines/LoggingBehaviour.cs | 35 +++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 AspNetCoreHero.Boilerplate.Application/Pipelines/LoggingBehaviour.cs diff --git a/AspNetCoreHero.Boilerplate.Api/appsettings.json b/AspNetCoreHero.Boilerplate.Api/appsettings.json index 2e7ffce4..dc9910e4 100644 --- a/AspNetCoreHero.Boilerplate.Api/appsettings.json +++ b/AspNetCoreHero.Boilerplate.Api/appsettings.json @@ -1,5 +1,5 @@ { - "UseInMemoryDatabase": false, + "UseInMemoryDatabase": true, "ConnectionStrings": { "ApplicationConnection": "Data Source=Server=(localdb)\\mssqllocaldb;Initial Catalog=AspNetCoreHero.Boilerplate;Integrated Security=True;MultipleActiveResultSets=True", "IdentityConnection": "Data Source=(localdb)\\mssqllocaldb;Initial Catalog=AspNetCoreHero.Boilerplate;Integrated Security=True;MultipleActiveResultSets=True" diff --git a/AspNetCoreHero.Boilerplate.Application/Extensions/ServiceCollectionExtensions.cs b/AspNetCoreHero.Boilerplate.Application/Extensions/ServiceCollectionExtensions.cs index 407a5eb4..7aa1bb53 100644 --- a/AspNetCoreHero.Boilerplate.Application/Extensions/ServiceCollectionExtensions.cs +++ b/AspNetCoreHero.Boilerplate.Application/Extensions/ServiceCollectionExtensions.cs @@ -1,4 +1,5 @@ -using AutoMapper; +using AspNetCoreHero.Boilerplate.Application.Pipelines; +using AutoMapper; using FluentValidation; using MediatR; using Microsoft.Extensions.DependencyInjection; @@ -14,6 +15,8 @@ public static void AddApplicationLayer(this IServiceCollection services) services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly()); services.AddMediatR(Assembly.GetExecutingAssembly()); //services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>)); + services.AddTransient(typeof(IPipelineBehavior<,>), typeof(LoggingBehaviour<,>)); + } } } \ No newline at end of file diff --git a/AspNetCoreHero.Boilerplate.Application/Pipelines/LoggingBehaviour.cs b/AspNetCoreHero.Boilerplate.Application/Pipelines/LoggingBehaviour.cs new file mode 100644 index 00000000..9440e812 --- /dev/null +++ b/AspNetCoreHero.Boilerplate.Application/Pipelines/LoggingBehaviour.cs @@ -0,0 +1,35 @@ +using MediatR; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace AspNetCoreHero.Boilerplate.Application.Pipelines +{ + public class LoggingBehaviour : IPipelineBehavior + { + private readonly ILogger _logger; + + public LoggingBehaviour(ILogger logger) + { + _logger = logger; + } + + public async Task Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate next) + { + try + { + var requestName = typeof(TRequest).Name; + _logger.LogInformation("Request: {Name}", requestName); + return await next(); + } + catch (Exception ex) + { + throw ex; + } + } + } +} From 8fbe6615441fe8985f6b2da0fdb884c2642afb27 Mon Sep 17 00:00:00 2001 From: yurekliisa Date: Fri, 22 Jan 2021 18:33:32 +0300 Subject: [PATCH 2/4] write error log when throw exceptions --- .../Services/AuthenticatedUserService.cs | 2 ++ .../Commands/Create/CreateBrandCommand.cs | 4 +++- .../Pipelines/LoggingBehaviour.cs | 19 +++++++++++++------ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/AspNetCoreHero.Boilerplate.Api/Services/AuthenticatedUserService.cs b/AspNetCoreHero.Boilerplate.Api/Services/AuthenticatedUserService.cs index 581b4c51..a5a056cc 100644 --- a/AspNetCoreHero.Boilerplate.Api/Services/AuthenticatedUserService.cs +++ b/AspNetCoreHero.Boilerplate.Api/Services/AuthenticatedUserService.cs @@ -1,5 +1,6 @@ using AspNetCoreHero.Boilerplate.Application.Interfaces.Shared; using Microsoft.AspNetCore.Http; +using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; namespace AspNetCoreHero.Boilerplate.Api.Services @@ -9,6 +10,7 @@ public class AuthenticatedUserService : IAuthenticatedUserService public AuthenticatedUserService(IHttpContextAccessor httpContextAccessor) { UserId = httpContextAccessor.HttpContext?.User?.FindFirstValue("uid"); + } public string UserId { get; } diff --git a/AspNetCoreHero.Boilerplate.Application/Features/Brands/Commands/Create/CreateBrandCommand.cs b/AspNetCoreHero.Boilerplate.Application/Features/Brands/Commands/Create/CreateBrandCommand.cs index deac341d..d41124d4 100644 --- a/AspNetCoreHero.Boilerplate.Application/Features/Brands/Commands/Create/CreateBrandCommand.cs +++ b/AspNetCoreHero.Boilerplate.Application/Features/Brands/Commands/Create/CreateBrandCommand.cs @@ -1,4 +1,5 @@ -using AspNetCoreHero.Boilerplate.Application.Interfaces.Repositories; +using AspNetCoreHero.Boilerplate.Application.Exceptions; +using AspNetCoreHero.Boilerplate.Application.Interfaces.Repositories; using AspNetCoreHero.Boilerplate.Domain.Entities.Catalog; using AspNetCoreHero.Results; using AutoMapper; @@ -31,6 +32,7 @@ public CreateBrandCommandHandler(IBrandRepository brandRepository, IUnitOfWork u public async Task> Handle(CreateBrandCommand request, CancellationToken cancellationToken) { + throw new ApiException("Error"); var product = _mapper.Map(request); await _brandRepository.InsertAsync(product); await _unitOfWork.Commit(cancellationToken); diff --git a/AspNetCoreHero.Boilerplate.Application/Pipelines/LoggingBehaviour.cs b/AspNetCoreHero.Boilerplate.Application/Pipelines/LoggingBehaviour.cs index 9440e812..8f437049 100644 --- a/AspNetCoreHero.Boilerplate.Application/Pipelines/LoggingBehaviour.cs +++ b/AspNetCoreHero.Boilerplate.Application/Pipelines/LoggingBehaviour.cs @@ -1,4 +1,5 @@ -using MediatR; +using AspNetCoreHero.Boilerplate.Application.Interfaces.Shared; +using MediatR; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -12,23 +13,29 @@ namespace AspNetCoreHero.Boilerplate.Application.Pipelines public class LoggingBehaviour : IPipelineBehavior { private readonly ILogger _logger; + private readonly IAuthenticatedUserService _authenticatedUserService; - public LoggingBehaviour(ILogger logger) + public LoggingBehaviour( + ILogger logger, + IAuthenticatedUserService authenticatedUserService + ) { _logger = logger; + _authenticatedUserService = authenticatedUserService; } public async Task Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate next) { + var requestName = typeof(TRequest).Name; try { - var requestName = typeof(TRequest).Name; - _logger.LogInformation("Request: {Name}", requestName); + _logger.LogInformation("{Name} Request From User {UserId}-{UserName}", requestName, _authenticatedUserService.UserId, _authenticatedUserService.Username); return await next(); } catch (Exception ex) - { - throw ex; + { + _logger.LogError("{Name} Request Exception Message : {Error} From User {UserId}-{UserName}", requestName,ex.Message,_authenticatedUserService.UserId,_authenticatedUserService.Username); + throw; } } } From c783b28f461df183fcf669778f241ad828d4aa6d Mon Sep 17 00:00:00 2001 From: yurekliisa Date: Fri, 22 Jan 2021 18:38:51 +0300 Subject: [PATCH 3/4] set username --- .../Services/AuthenticatedUserService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AspNetCoreHero.Boilerplate.Api/Services/AuthenticatedUserService.cs b/AspNetCoreHero.Boilerplate.Api/Services/AuthenticatedUserService.cs index a5a056cc..e6987e0c 100644 --- a/AspNetCoreHero.Boilerplate.Api/Services/AuthenticatedUserService.cs +++ b/AspNetCoreHero.Boilerplate.Api/Services/AuthenticatedUserService.cs @@ -10,7 +10,7 @@ public class AuthenticatedUserService : IAuthenticatedUserService public AuthenticatedUserService(IHttpContextAccessor httpContextAccessor) { UserId = httpContextAccessor.HttpContext?.User?.FindFirstValue("uid"); - + Username = httpContextAccessor.HttpContext?.User?.FindFirstValue(ClaimTypes.NameIdentifier); } public string UserId { get; } From 8c16064030bb57ce9cadb0e089c9f0416afd40a1 Mon Sep 17 00:00:00 2001 From: yurekliisa Date: Fri, 22 Jan 2021 18:39:31 +0300 Subject: [PATCH 4/4] removed throw exception --- .../Features/Brands/Commands/Create/CreateBrandCommand.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/AspNetCoreHero.Boilerplate.Application/Features/Brands/Commands/Create/CreateBrandCommand.cs b/AspNetCoreHero.Boilerplate.Application/Features/Brands/Commands/Create/CreateBrandCommand.cs index d41124d4..73ddf598 100644 --- a/AspNetCoreHero.Boilerplate.Application/Features/Brands/Commands/Create/CreateBrandCommand.cs +++ b/AspNetCoreHero.Boilerplate.Application/Features/Brands/Commands/Create/CreateBrandCommand.cs @@ -32,7 +32,6 @@ public CreateBrandCommandHandler(IBrandRepository brandRepository, IUnitOfWork u public async Task> Handle(CreateBrandCommand request, CancellationToken cancellationToken) { - throw new ApiException("Error"); var product = _mapper.Map(request); await _brandRepository.InsertAsync(product); await _unitOfWork.Commit(cancellationToken);