Skip to content

Instantly share code, notes, and snippets.

@danielplawgo
Created July 8, 2018 05:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielplawgo/93a7f3c699da9073d7c9fa1ae30c584f to your computer and use it in GitHub Desktop.
Save danielplawgo/93a7f3c699da9073d7c9fa1ae30c584f to your computer and use it in GitHub Desktop.
Log using action filters
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new LogFilterAttribute());
}
}
public class LogFilterAttribute : ActionFilterAttribute, IActionFilter
{
private Stopwatch _stopwatch;
private static Logger _logger = LogManager.GetCurrentClassLogger();
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
_logger.Info(string.Format("Request ({0}) start at {1}", filterContext.HttpContext.Request.Url.AbsolutePath, DateTime.Now));
_stopwatch = Stopwatch.StartNew();
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
_stopwatch.Stop();
_logger.Info(string.Format("Request ({0}) end at {1} - time {2}", filterContext.HttpContext.Request.Url.AbsolutePath, DateTime.Now,
_stopwatch.ElapsedMilliseconds));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment