add LoggerBehavior
This commit is contained in:
parent
e3584be685
commit
44b257aaf7
@ -4,7 +4,8 @@
|
|||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
"Microsoft": "Warning",
|
"Microsoft": "Warning",
|
||||||
"Microsoft.Hosting.Lifetime": "Information",
|
"Microsoft.Hosting.Lifetime": "Information",
|
||||||
"Microsoft.EntityFrameworkCore": "Information"
|
"Microsoft.EntityFrameworkCore": "Information",
|
||||||
|
"HN.Application": "Information"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
31
Infrastructure/Behaviors/LoggerBehavior.cs
Normal file
31
Infrastructure/Behaviors/LoggerBehavior.cs
Normal file
@ -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<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
|
||||||
|
{
|
||||||
|
private readonly ILoggerFactory _factory;
|
||||||
|
|
||||||
|
public LoggerBehavior(ILoggerFactory factory)
|
||||||
|
{
|
||||||
|
_factory = factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -33,6 +33,7 @@ namespace HN.Infrastructure
|
|||||||
services.AddScoped<ILinkRepository, LinkRepository>();
|
services.AddScoped<ILinkRepository, LinkRepository>();
|
||||||
services.AddScoped<ICommentRepository, CommentRepository>();
|
services.AddScoped<ICommentRepository, CommentRepository>();
|
||||||
services.AddScoped(typeof(IPipelineBehavior<,>), typeof(UnitOfWorkBehavior<,>));
|
services.AddScoped(typeof(IPipelineBehavior<,>), typeof(UnitOfWorkBehavior<,>));
|
||||||
|
services.AddSingleton(typeof(IPipelineBehavior<,>), typeof(LoggerBehavior<,>));
|
||||||
services.AddMediatR(typeof(HN.Application.IHNContext));
|
services.AddMediatR(typeof(HN.Application.IHNContext));
|
||||||
|
|
||||||
return new HNServicesBuilder(services);
|
return new HNServicesBuilder(services);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user