Skip to content

Instantly share code, notes, and snippets.

@sametoz
Created May 3, 2020 08:15
Show Gist options
  • Save sametoz/092850e29f1b505cb6e08161a91ffc33 to your computer and use it in GitHub Desktop.
Save sametoz/092850e29f1b505cb6e08161a91ffc33 to your computer and use it in GitHub Desktop.
public class LogContextMiddleware
{
private readonly RequestDelegate next;
public LogContextMiddleware(RequestDelegate next)
{
this.next = next;
}
public Task Invoke(HttpContext context)
{
/*
LogContext, Serilog'un Context kayıtlarını denetler.
PushProperty ile parametreleri çekip, Log mekanizmasına yönlendirebilirsin.
İlk parametre Property'nin adını, ikinci parametre ise context'teki property'yi ifade eder.
ÖNEMLİ!: Döndürülen property adı, log konfigürasyonundaki sütun ismiyle aynı olmalıdır.
*/
LogContext.PushProperty("UserName", context.User.Identity.Name);
LogContext.PushProperty("ApiPath", context.Request.Path);
return next(context);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment