Modifica

Middleware

Middleware is a useful tool for logging, validation, and more. You can easily register your own middleware using the app.Use method.

Middleware is a useful tool for logging, validation, and more. You can easily register your own middleware using the app.use method.

Below is an example of a middleware that will log the elapse time of all handlers that come after it.

app.Use(async context =>
{
    var start = DateTime.UtcNow;
    try
    {
        await context.Next();
    } catch
    {
        context.Log.Error("error occurred during activity processing");
    }
    context.Log.Debug($"request took {(DateTime.UtcNow - start).TotalMilliseconds}ms");
});
@app.use
async def log_activity(ctx: ActivityContext[MessageActivity]):
    started_at = datetime.now()
    await ctx.next()
    ctx.logger.debug(f"{datetime.now() - started_at}")
app.use(async ({ log, next }) => {
  const startedAt = new Date();
  await next();
  log.debug(new Date().getTime() - startedAt.getTime());
});