Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
feat(enrolling): enable logging every command and query processing
Browse files Browse the repository at this point in the history
  • Loading branch information
ratanparai committed Jun 27, 2020
1 parent 70114d7 commit 0ac94ec
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
@@ -0,0 +1,38 @@
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Microsoft.Extensions.Logging;

namespace OpenCodeFoundation.ESchool.Services.Enrolling.API.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 ?? throw new System.ArgumentNullException(nameof(logger));
}

public async Task<TResponse> Handle(
TRequest request,
CancellationToken cancellationToken,
RequestHandlerDelegate<TResponse> next)
{
_logger.LogInformation(
"Handling request {RequestName} ({@Request})",
request.GetType().Name,
request);

var response = await next();

_logger.LogInformation(
"Request {RequestName} handled. Response: {@Response}",
request.GetType().Name,
response);

return response;
}
}
}
3 changes: 3 additions & 0 deletions src/Services/Enrolling/Enrolling.API/Startup.cs
Expand Up @@ -11,6 +11,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using OpenCodeFoundation.ESchool.Services.Enrolling.API.Application.Behaviors;
using OpenCodeFoundation.ESchool.Services.Enrolling.API.Application.Validations;
using OpenCodeFoundation.ESchool.Services.Enrolling.API.Extensions;
using OpenCodeFoundation.ESchool.Services.Enrolling.Infrastructure;
Expand All @@ -31,6 +32,8 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddMediatR(typeof(Startup).GetTypeInfo().Assembly);

services.AddTransient(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>));

services.AddDbContext<EnrollingContext>(options =>
{
options.UseSqlServer(
Expand Down

0 comments on commit 0ac94ec

Please sign in to comment.