Add logging

This commit is contained in:
2016-09-14 09:55:59 -04:00
parent 052f812d6f
commit 521ceda710
10 changed files with 57 additions and 17 deletions
@@ -1,27 +1,26 @@
using System.Linq;
using System.Web.Mvc;
using InventoryTraker.Web.Utilities;
using NLog;
namespace InventoryTraker.Web.Attributes
{
public class ActionLogAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
public override void OnActionExecuting(ActionExecutingContext ctx)
{
if (filterContext != null)
if (ctx != null)
{
var controller = filterContext.RouteData.Values["controller"].ToString();
var action = filterContext.RouteData.Values["action"].ToString();
var username = filterContext.HttpContext.User.Identity.Name;
var loggerName = $"{controller}Controller.{action}";
var @params = string.Join(", ", filterContext.ActionParameters.Select(i => $"{i.Key}: {{{i.Value}}}"));
var loggerName = ctx.GetLoggerName();
var username = ctx.HttpContext.User.Identity.Name;
var @params = string.Join(", ", ctx.ActionParameters.Select(i => $"{i.Key}: {{{i.Value}}}"));
var hostAddress = filterContext.HttpContext.Request.UserHostAddress;
var hostAddress = ctx.HttpContext.Request.UserHostAddress;
LogManager.GetLogger(loggerName)
.Info("UserHostAddress: {0}, username: {1}, params: {{{2}}}", hostAddress, username, @params);
}
base.OnActionExecuting(filterContext);
base.OnActionExecuting(ctx);
}
}
}