test logger method

master
Tudor Stanciu 2020-06-07 14:43:34 +03:00
parent 5503a82241
commit 76045fe755
4 changed files with 62 additions and 1 deletions

View File

@ -0,0 +1,34 @@
using Chatbot.Api.Application.Commands;
using Chatbot.Api.Application.Events;
using MediatR;
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Chatbot.Api.Application.CommandHandlers
{
public class TestLoggerHandler : IRequestHandler<TestLogger, LoggerTested>
{
private readonly ILogger<TestLoggerHandler> _logger;
public TestLoggerHandler(ILogger<TestLoggerHandler> logger)
{
_logger = logger;
}
public async Task<LoggerTested> Handle(TestLogger request, CancellationToken cancellationToken)
{
_logger.LogDebug(TextToLog);
_logger.LogInformation(TextToLog);
_logger.LogWarning(TextToLog);
_logger.LogError(TextToLog);
_logger.LogCritical(TextToLog);
await Task.Delay(1);
return new LoggerTested();
}
private string TextToLog => $"Logger test - {DateTime.Now}";
}
}

View File

@ -0,0 +1,13 @@
using Chatbot.Api.Application.Events;
using System;
namespace Chatbot.Api.Application.Commands
{
public class TestLogger : Command<LoggerTested>
{
public TestLogger()
: base(new Metadata() { CorrelationId = Guid.NewGuid() })
{
}
}
}

View File

@ -0,0 +1,6 @@
namespace Chatbot.Api.Application.Events
{
public class LoggerTested
{
}
}

View File

@ -1,4 +1,5 @@
using Chatbot.Api.Application.Queries; using Chatbot.Api.Application.Commands;
using Chatbot.Api.Application.Queries;
using MediatR; using MediatR;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@ -38,5 +39,12 @@ namespace Chatbot.Api.Controllers
var result = await _mediator.Send(query); var result = await _mediator.Send(query);
return Ok(result); return Ok(result);
} }
[HttpPost("test-logger")]
public async Task<IActionResult> CloseChat([FromBody] TestLogger closeChat)
{
var result = await _mediator.Send(closeChat);
return Ok(result);
}
} }
} }