Skip to content

Commit

Permalink
logs
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigojap committed Feb 3, 2021
1 parent 0a69d04 commit e788eb7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
8 changes: 7 additions & 1 deletion VirtualMind.Application/Commands/CreateOperationCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.EntityFrameworkCore;
using VirtualMind.Application.Exceptions;
using System.Globalization;
using Microsoft.Extensions.Logging;

namespace VirtualMind.Application.Commands
{
Expand All @@ -28,12 +29,15 @@ public class CreateOperationCommandHandler : IRequestHandler<CreateOperationComm
{
private readonly IVirtualMindDbContext VirtualMindDbContext;
private readonly ICurrencyExchangeFactory CurrencyExchangeFactory;
private readonly ILogger<CreateOperationCommandHandler> Logger;

public CreateOperationCommandHandler(IVirtualMindDbContext virtualMindDbContext,
ICurrencyExchangeFactory currencyExchangeFactory)
ICurrencyExchangeFactory currencyExchangeFactory,
ILogger<CreateOperationCommandHandler> logger)
{
VirtualMindDbContext = virtualMindDbContext;
CurrencyExchangeFactory = currencyExchangeFactory;
this.Logger = logger;
}

public async Task<int> Handle(CreateOperationCommand request, CancellationToken cancellationToken)
Expand Down Expand Up @@ -88,6 +92,8 @@ private async Task CheckLimitOperation(int userId, decimal purchasedAmount, Curr

if (total > currencyParameters.Limit)
{
Logger.LogInformation($"InvalidOperation[CheckLimitOperation] - user {userId} - at: {DateTime.Now}");

throw new ValidationException("InvalidOperation", new[]
{
$"This operation is not allowed because it exceeds the limits for the currency [{currency}] and user.",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using MediatR;
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace VirtualMind.Application.Commom.Middlewares
{
public class UnhandledExceptionBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
private readonly ILogger<TRequest> _logger;

public UnhandledExceptionBehaviour(ILogger<TRequest> logger)
{
_logger = logger;
}

public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
{
try
{
return await next();
}
catch (Exception ex)
{
var requestName = typeof(TRequest).Name;

_logger.LogError(ex, "You got an error: Unhandled Exception for Request {Name} {@Request}", requestName, request);

throw;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public static class ServiceCollectionConfiguration
public static IServiceCollection AddApplicationDI(this IServiceCollection services)
{
services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());
services.AddMediatR(Assembly.GetExecutingAssembly());
services.AddMediatR(Assembly.GetExecutingAssembly());

services.AddTransient(typeof(IPipelineBehavior<,>), typeof(UnhandledExceptionBehaviour<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehaviour<,>));

services.AddScoped<ICurrencyExchangeFactory, GetCurrencyExchangeFactory>();
Expand Down
9 changes: 3 additions & 6 deletions VirtualMind.WebApp/Controllers/ExchangeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ namespace VirtualMind.WebApp.Controllers
[ApiController]
[Route("[controller]")]
public class ExchangeController : ControllerBase
{
private readonly ILogger<ExchangeController> _logger;
{
private readonly IMediator _mediator;

public ExchangeController(ILogger<ExchangeController> logger,
IMediator mediator)
{
_logger = logger;
public ExchangeController(IMediator mediator)
{
_mediator = mediator;
}

Expand Down

0 comments on commit e788eb7

Please sign in to comment.