添加serilog日志支持和部分其他efcore代码
This commit is contained in:
24
QRCodeService/Application/Behaviors/LoggingBehavior.cs
Normal file
24
QRCodeService/Application/Behaviors/LoggingBehavior.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using QRCodeService.Extensions;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace QRCodeService.Application.Behaviors
|
||||
{
|
||||
public class LoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
|
||||
{
|
||||
private readonly ILogger<LoggingBehavior<TRequest, TResponse>> _logger;
|
||||
public LoggingBehavior(ILogger<LoggingBehavior<TRequest, TResponse>> logger) => _logger = logger;
|
||||
|
||||
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
|
||||
{
|
||||
_logger.LogInformation("----- Handling command {CommandName} ({@Command})", request.GetGenericTypeName(), request);
|
||||
var response = await next();
|
||||
_logger.LogInformation("----- Command {CommandName} handled - response: {@Response}", request.GetGenericTypeName(), response);
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
}
|
||||
73
QRCodeService/Application/Behaviors/TransactionBehaviour.cs
Normal file
73
QRCodeService/Application/Behaviors/TransactionBehaviour.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using Infrastructure;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using QRCodeService.Extensions;
|
||||
using Serilog.Context;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace QRCodeService.Application.Behaviors
|
||||
{
|
||||
public class TransactionBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
|
||||
{
|
||||
private readonly ILogger<TransactionBehaviour<TRequest, TResponse>> _logger;
|
||||
private readonly AppDbContext _dbContext;
|
||||
//private readonly IOrderingIntegrationEventService _orderingIntegrationEventService;
|
||||
|
||||
public TransactionBehaviour(AppDbContext dbContext,
|
||||
//IOrderingIntegrationEventService orderingIntegrationEventService,
|
||||
ILogger<TransactionBehaviour<TRequest, TResponse>> logger)
|
||||
{
|
||||
_dbContext = dbContext ?? throw new ArgumentException(nameof(AppDbContext));
|
||||
//_orderingIntegrationEventService = orderingIntegrationEventService ?? throw new ArgumentException(nameof(orderingIntegrationEventService));
|
||||
_logger = logger ?? throw new ArgumentException(nameof(ILogger));
|
||||
}
|
||||
|
||||
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
|
||||
{
|
||||
var response = default(TResponse);
|
||||
var typeName = request.GetGenericTypeName();
|
||||
|
||||
try
|
||||
{
|
||||
if (_dbContext.HasActiveTransaction)
|
||||
{
|
||||
return await next();
|
||||
}
|
||||
|
||||
var strategy = _dbContext.Database.CreateExecutionStrategy();
|
||||
|
||||
await strategy.ExecuteAsync(async () =>
|
||||
{
|
||||
Guid transactionId;
|
||||
|
||||
using (var transaction = await _dbContext.BeginTransactionAsync())
|
||||
using (LogContext.PushProperty("TransactionContext", transaction.TransactionId))
|
||||
{
|
||||
_logger.LogInformation("----- Begin transaction {TransactionId} for {CommandName} ({@Command})", transaction.TransactionId, typeName, request);
|
||||
|
||||
response = await next();
|
||||
|
||||
_logger.LogInformation("----- Commit transaction {TransactionId} for {CommandName}", transaction.TransactionId, typeName);
|
||||
|
||||
await _dbContext.CommitTransactionAsync(transaction);
|
||||
|
||||
transactionId = transaction.TransactionId;
|
||||
}
|
||||
|
||||
//await _orderingIntegrationEventService.PublishEventsThroughEventBusAsync(transactionId);
|
||||
});
|
||||
|
||||
return response;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "ERROR Handling transaction for {CommandName} ({@Command})", typeName, request);
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
45
QRCodeService/Application/Behaviors/ValidatorBehavior.cs
Normal file
45
QRCodeService/Application/Behaviors/ValidatorBehavior.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using QRCodeService.Extensions;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace QRCodeService.Application.Behaviors
|
||||
{
|
||||
public class ValidatorBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
|
||||
{
|
||||
private readonly ILogger<ValidatorBehavior<TRequest, TResponse>> _logger;
|
||||
private readonly IValidator<TRequest>[] _validators;
|
||||
|
||||
public ValidatorBehavior(IValidator<TRequest>[] validators, ILogger<ValidatorBehavior<TRequest, TResponse>> logger)
|
||||
{
|
||||
_validators = validators;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
|
||||
{
|
||||
var typeName = request.GetGenericTypeName();
|
||||
|
||||
_logger.LogInformation("----- Validating command {CommandType}", typeName);
|
||||
|
||||
var failures = _validators
|
||||
.Select(v => v.Validate(request))
|
||||
.SelectMany(result => result.Errors)
|
||||
.Where(error => error != null)
|
||||
.ToList();
|
||||
|
||||
if (failures.Any())
|
||||
{
|
||||
_logger.LogWarning("Validation errors - {CommandType} - Command: {@Command} - Errors: {@ValidationErrors}", typeName, request, failures);
|
||||
|
||||
//throw new OrderingDomainException(
|
||||
// $"Command Validation Errors for type {typeof(TRequest).Name}", new ValidationException("Validation exception", failures));
|
||||
}
|
||||
|
||||
return await next();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user