This article will present how you can add a pipeline behavior to MediatR and then log to SeriLog and Seq. This makes it possible to add for example logging and use the 'decorator pattern' with MediatR to add
behavior to the pipeline of Asp.net core in general (.net 6 is used here). It is therefore sort of Middelware and tied together via MediatR.
First off, lets define the IPipelineBehavior, which will do our logging. Note that you have to add the generic type parameter where condition here of TRequest.
Note that we inject the ILogger<LoggingBehavior<TRequest, TResponse>> logger object here. In .net (core) we send in ILogger instance of the type of the class we are injecting into to log out to log sources and
SeriLog will present this information. Inside Seq we can also browse this log information.
We only log payloads if we are inside Development environment and the payload of request / response can be serialized and is below 50 kB.
Program.cs
using MediatR;
using Newtonsoft.Json;
using System.Diagnostics;
namespace CqrsDemoWebApi.Features.Books.Pipeline
{
public class LoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : IRequest<TResponse>
{
private readonly ILogger<LoggingBehavior<TRequest, TResponse>> _logger;
private readonly IWebHostEnvironment _environment;
public LoggingBehavior(ILogger<LoggingBehavior<TRequest, TResponse>> logger, IWebHostEnvironment environment)
{
_logger = logger;
_environment = environment;
}
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
{
string? requestData = null;
if (_environment.IsDevelopment())
{
//log serializable payload data in request (as json format) to SeriLog if we are inside Development environment and the payload is below 50 kB in serializable size
try
{
requestData = request != null ? JsonConvert.SerializeObject(request) : null;
if (requestData?.Length > 50 * 1028)
requestData = null; //avoid showing data larger than 100 kB in serialized form in the logs of Seq / SeriLog
}
catch (Exception)
{
//ignore
}
}
_logger.LogInformation($"Handling request {typeof(TRequest).Name} {{@requestData}}", requestData);
var response = await next();
string? responseData = null;
if (_environment.IsDevelopment())
{
//log serializable payload data in response (as json format) to SeriLog if we are in Development environment and the payload is below 50 kB in serialized size
try
{
responseData = response != null ? JsonConvert.SerializeObject(response) : null;
if (responseData?.Length > 50 * 1028)
responseData = null; //avoid showing data larger than 100 kB in serialized form in the logs of Seq / SeriLog
}
catch (Exception)
{
//ignore
}
}
_logger.LogInformation($"Handled request {typeof(TRequest).Name}. Returning result of type {typeof(TResponse).Name} {{@responseData}}.", responseData);
return response;
}
}
}
Afterwards register the IPipelineBehavior. Note how we also add UseSerilog method here and add Seq too.
Program.cs
using CqrsDemoWebApi.Database;
using CqrsDemoWebApi.Features.Books.Pipeline;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Serilog;
using System.Reflection;
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseSerilog((ctx, lc) =>
lc.WriteTo.Console()
.WriteTo.Seq("http://localhost:5341")); //note - to use Seq with this url - install it from here (free individual license) : https://datalust.co/download
// Add services to the container.
builder.Services.AddMediatR(Assembly.GetExecutingAssembly()); //adding MediatR support here.
//register some pipeline(s) defined for MediatR usage
builder.Services.AddTransient(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>));
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var connectionString = new ConnectionString(builder.Configuration.GetConnectionString("CqrsDemoBooksDb"));
builder.Services.AddSingleton(connectionString);
builder.Services.AddDbContext<BooksDbContext>(options =>
options.UseSqlServer(connectionString.Value));
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
Here are the updated nuget packages inside the csproj file of the Asp.net core project (.net 6) :
<ItemGroup>
<PackageReference Include="Mediatr" Version="10.0.1" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="10.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Serilog.AspNetCore" Version="6.0.0-dev-00265" />
<PackageReference Include="Serilog.Sinks.Seq" Version="5.1.2-dev-00225" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>
We also need to install Seq on our computer to see the messages sent to SeriLog inside Seq.
Go to this url to download free individual license of Seq :
https://datalust.co/download
You can see the logs at Seq's default url and also set up in Program.cs file :
http://localhost:5341
Share this article on LinkedIn.
No comments:
Post a Comment