diff --git a/Apps/Website/appsettings.Development.json b/Apps/Website/appsettings.Development.json index eaa8762..dd829bf 100644 --- a/Apps/Website/appsettings.Development.json +++ b/Apps/Website/appsettings.Development.json @@ -4,7 +4,8 @@ "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information", - "Microsoft.EntityFrameworkCore": "Information" + "Microsoft.EntityFrameworkCore": "Information", + "HN.Application": "Information" } } } diff --git a/Infrastructure/Behaviors/LoggerBehavior.cs b/Infrastructure/Behaviors/LoggerBehavior.cs new file mode 100644 index 0000000..4b2bdf4 --- /dev/null +++ b/Infrastructure/Behaviors/LoggerBehavior.cs @@ -0,0 +1,31 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using MediatR; +using Microsoft.Extensions.Logging; + +namespace HN.Infrastructure.Behaviors +{ + public sealed class LoggerBehavior : IPipelineBehavior + { + private readonly ILoggerFactory _factory; + + public LoggerBehavior(ILoggerFactory factory) + { + _factory = factory; + } + + public async Task Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate next) + { + var logger = _factory.CreateLogger(request.GetType()); + + logger.LogInformation("Processing request {0} at {1}", request, DateTime.Now); + + var response = await next(); + + logger.LogInformation("Processed request {0} at {1}", request, DateTime.Now); + + return response; + } + } +} \ No newline at end of file diff --git a/Infrastructure/ServiceCollectionExtensions.cs b/Infrastructure/ServiceCollectionExtensions.cs index e88b93e..50284ef 100644 --- a/Infrastructure/ServiceCollectionExtensions.cs +++ b/Infrastructure/ServiceCollectionExtensions.cs @@ -33,6 +33,7 @@ namespace HN.Infrastructure services.AddScoped(); services.AddScoped(); services.AddScoped(typeof(IPipelineBehavior<,>), typeof(UnitOfWorkBehavior<,>)); + services.AddSingleton(typeof(IPipelineBehavior<,>), typeof(LoggerBehavior<,>)); services.AddMediatR(typeof(HN.Application.IHNContext)); return new HNServicesBuilder(services);