diff --git a/.gitignore b/.gitignore index 9b6049e..f13d810 100644 --- a/.gitignore +++ b/.gitignore @@ -28,4 +28,5 @@ obj/ _ReSharper*/ [Tt]est[Rr]esult* packages/**/ -**/App_Data/* \ No newline at end of file +**/App_Data/* +*/Logs/* diff --git a/InventoryTraker.Web/Attributes/ActionLogAttribute.cs b/InventoryTraker.Web/Attributes/ActionLogAttribute.cs index 4493902..eccde0e 100644 --- a/InventoryTraker.Web/Attributes/ActionLogAttribute.cs +++ b/InventoryTraker.Web/Attributes/ActionLogAttribute.cs @@ -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); } } } \ No newline at end of file diff --git a/InventoryTraker.Web/Controllers/AuthenticationController.cs b/InventoryTraker.Web/Controllers/AuthenticationController.cs index fbd4c5b..cf7a068 100644 --- a/InventoryTraker.Web/Controllers/AuthenticationController.cs +++ b/InventoryTraker.Web/Controllers/AuthenticationController.cs @@ -1,5 +1,6 @@ using System.Threading.Tasks; using System.Web.Mvc; +using InventoryTraker.Web.Attributes; using InventoryTraker.Web.Identity; using InventoryTraker.Web.Models; using Microsoft.AspNet.Identity; @@ -26,6 +27,7 @@ namespace InventoryTraker.Web.Controllers } [HttpPost] + [ActionLog] public async Task Login(LoginForm form) { var user = await _userManager.FindByEmailAsync(form.EmailAddress); @@ -43,6 +45,7 @@ namespace InventoryTraker.Web.Controllers return Json(true); } + [ActionLog] public ActionResult Logout() { _authManager.SignOut(); diff --git a/InventoryTraker.Web/Controllers/ControllerBase.cs b/InventoryTraker.Web/Controllers/ControllerBase.cs index d03419f..9e551fb 100644 --- a/InventoryTraker.Web/Controllers/ControllerBase.cs +++ b/InventoryTraker.Web/Controllers/ControllerBase.cs @@ -1,18 +1,31 @@ -using System.Collections.Generic; -using System.Linq; +using System.Linq; using System.Web.Mvc; using InventoryTraker.Web.ActionResults; +using InventoryTraker.Web.Utilities; +using NLog; namespace InventoryTraker.Web.Controllers { public abstract class ControllerBase : Controller { - public BetterJsonResult BetterJson(T model) + protected override void OnException(ExceptionContext ctx) + { + if (ctx?.Exception != null) + { + var loggerName = ctx.GetLoggerName(); + + LogManager.GetLogger(loggerName).Error(ctx.Exception); + } + + base.OnException(ctx); + } + + protected BetterJsonResult BetterJson(T model) { return new BetterJsonResult {Data = model}; } - protected string[] GetModelStateErrorList() + private string[] GetModelStateErrorList() { var errorList = from kvp in ModelState diff --git a/InventoryTraker.Web/Controllers/InventoryController.cs b/InventoryTraker.Web/Controllers/InventoryController.cs index 91d374b..2c772f9 100644 --- a/InventoryTraker.Web/Controllers/InventoryController.cs +++ b/InventoryTraker.Web/Controllers/InventoryController.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Web.Mvc; using AutoMapper; using AutoMapper.QueryableExtensions; +using InventoryTraker.Web.Attributes; using InventoryTraker.Web.Core; using InventoryTraker.Web.Data; using InventoryTraker.Web.Models; @@ -49,6 +50,7 @@ namespace InventoryTraker.Web.Controllers .OrderBy(x => x.InventoryType.Name); } + [ActionLog] public JsonResult Add(InventoryAddForm form) { if (!ModelState.IsValid) @@ -79,6 +81,7 @@ namespace InventoryTraker.Web.Controllers return BetterJson(model); } + [ActionLog] public JsonResult Distribute(InventoryDistributeForm form) { if (!ModelState.IsValid) @@ -136,6 +139,7 @@ namespace InventoryTraker.Web.Controllers .ToArray()); } + [ActionLog] public JsonResult Remove(InventoryRemoveForm form) { if (!ModelState.IsValid) diff --git a/InventoryTraker.Web/Controllers/TransactionController.cs b/InventoryTraker.Web/Controllers/TransactionController.cs index 8856c23..c541f3d 100644 --- a/InventoryTraker.Web/Controllers/TransactionController.cs +++ b/InventoryTraker.Web/Controllers/TransactionController.cs @@ -1,8 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Linq; +using System.Linq; using System.Web.Mvc; using AutoMapper.QueryableExtensions; +using InventoryTraker.Web.Attributes; using InventoryTraker.Web.Core; using InventoryTraker.Web.Data; using InventoryTraker.Web.Models; @@ -55,6 +54,7 @@ namespace InventoryTraker.Web.Controllers return BetterJson(new { totalItems, transactions }); } + [ActionLog] public JsonResult Delete(int transactionId) { var transaction = _context.Transactions.Find(transactionId); diff --git a/InventoryTraker.Web/InventoryTraker.Web.csproj b/InventoryTraker.Web/InventoryTraker.Web.csproj index 87925e1..e9da99c 100644 --- a/InventoryTraker.Web/InventoryTraker.Web.csproj +++ b/InventoryTraker.Web/InventoryTraker.Web.csproj @@ -355,6 +355,7 @@ + diff --git a/InventoryTraker.Web/NLog.config b/InventoryTraker.Web/NLog.config index 68ae0ea..2c7f8f7 100644 --- a/InventoryTraker.Web/NLog.config +++ b/InventoryTraker.Web/NLog.config @@ -19,4 +19,8 @@ + + + + diff --git a/InventoryTraker.Web/Utilities/ControllerContextExtensions.cs b/InventoryTraker.Web/Utilities/ControllerContextExtensions.cs new file mode 100644 index 0000000..b83370e --- /dev/null +++ b/InventoryTraker.Web/Utilities/ControllerContextExtensions.cs @@ -0,0 +1,14 @@ +using System.Web.Mvc; + +namespace InventoryTraker.Web.Utilities +{ + public static class ControllerContextExtensions + { + public static string GetLoggerName(this ControllerContext ctx) + { + var controller = ctx.RouteData.Values["controller"].ToString(); + var action = ctx.RouteData.Values["action"].ToString(); + return $"{controller}Controller.{action}"; + } + } +} \ No newline at end of file diff --git a/InventoryTraker.Web/Utilities/InventoryTypeParser.cs b/InventoryTraker.Web/Utilities/InventoryTypeParser.cs index 6dfe304..f093aec 100644 --- a/InventoryTraker.Web/Utilities/InventoryTypeParser.cs +++ b/InventoryTraker.Web/Utilities/InventoryTypeParser.cs @@ -10,6 +10,7 @@ namespace InventoryTraker.Web.Utilities { private readonly FileSystemInfo _excelFile; + // ReSharper disable once ClassNeverInstantiated.Local private sealed class InventoryTypeMap : CsvClassMap { public InventoryTypeMap()