diff --git a/AspNetCoreHero.Boilerplate.Api/Services/AuthenticatedUserService.cs b/AspNetCoreHero.Boilerplate.Api/Services/AuthenticatedUserService.cs index 581b4c51..e6987e0c 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"); + Username = httpContextAccessor.HttpContext?.User?.FindFirstValue(ClaimTypes.NameIdentifier); } public string UserId { get; } 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/Features/Brands/Commands/Create/CreateBrandCommand.cs b/AspNetCoreHero.Boilerplate.Application/Features/Brands/Commands/Create/CreateBrandCommand.cs index deac341d..73ddf598 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; diff --git a/AspNetCoreHero.Boilerplate.Application/Pipelines/LoggingBehaviour.cs b/AspNetCoreHero.Boilerplate.Application/Pipelines/LoggingBehaviour.cs new file mode 100644 index 00000000..8f437049 --- /dev/null +++ b/AspNetCoreHero.Boilerplate.Application/Pipelines/LoggingBehaviour.cs @@ -0,0 +1,42 @@ +using AspNetCoreHero.Boilerplate.Application.Interfaces.Shared; +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; + private readonly IAuthenticatedUserService _authenticatedUserService; + + 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 + { + _logger.LogInformation("{Name} Request From User {UserId}-{UserName}", requestName, _authenticatedUserService.UserId, _authenticatedUserService.Username); + return await next(); + } + catch (Exception ex) + { + _logger.LogError("{Name} Request Exception Message : {Error} From User {UserId}-{UserName}", requestName,ex.Message,_authenticatedUserService.UserId,_authenticatedUserService.Username); + throw; + } + } + } +}