Skip to content

REQUESTS - Timed Logging

Ever wanted to track and log how long your requests take to process? Maybe even have an error logged if a call is passing a certain threshold? The TimedLogging middleware is designed to help you with this.

Internally, the middleware will log the time it takes to process a request to your logging setup. If the error threshold is passed, it will log an error otherwise it will log to Debug.

We recommend focusing timed logging on your most critical requests. Logging every request can be a performance hit.

Setup

In your host startup, add the following to your mediator configuration:

services.AddShinyMediator(cfg => cfg.UseTimedLogging());

To use it on with your request handlers, simply mark them with the TimedLoggingAttribute:

public class MyRequestHandler : IRequestHandler<MyRequest>
{
[TimedLogging(3000)] // If longer than 3000 milliseconds, log as an error
public async Task Execute(MyRequest request, CancellationToken ct)
{
// do your work
}
}