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);
		}
	}
}