Files
InventoryTracker/InventoryTraker.Web/Attributes/ActionLogAttribute.cs
T
2016-09-14 09:55:59 -04:00

26 lines
737 B
C#

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 ctx)
{
if (ctx != null)
{
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 = ctx.HttpContext.Request.UserHostAddress;
LogManager.GetLogger(loggerName)
.Info("UserHostAddress: {0}, username: {1}, params: {{{2}}}", hostAddress, username, @params);
}
base.OnActionExecuting(ctx);
}
}
}