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
+2 -1
View File
@@ -28,4 +28,5 @@ obj/
_ReSharper*/ _ReSharper*/
[Tt]est[Rr]esult* [Tt]est[Rr]esult*
packages/**/ packages/**/
**/App_Data/* **/App_Data/*
*/Logs/*
@@ -1,27 +1,26 @@
using System.Linq; using System.Linq;
using System.Web.Mvc; using System.Web.Mvc;
using InventoryTraker.Web.Utilities;
using NLog; using NLog;
namespace InventoryTraker.Web.Attributes namespace InventoryTraker.Web.Attributes
{ {
public class ActionLogAttribute : ActionFilterAttribute 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 loggerName = ctx.GetLoggerName();
var action = filterContext.RouteData.Values["action"].ToString(); var username = ctx.HttpContext.User.Identity.Name;
var username = filterContext.HttpContext.User.Identity.Name; var @params = string.Join(", ", ctx.ActionParameters.Select(i => $"{i.Key}: {{{i.Value}}}"));
var loggerName = $"{controller}Controller.{action}";
var @params = string.Join(", ", filterContext.ActionParameters.Select(i => $"{i.Key}: {{{i.Value}}}"));
var hostAddress = filterContext.HttpContext.Request.UserHostAddress; var hostAddress = ctx.HttpContext.Request.UserHostAddress;
LogManager.GetLogger(loggerName) LogManager.GetLogger(loggerName)
.Info("UserHostAddress: {0}, username: {1}, params: {{{2}}}", hostAddress, username, @params); .Info("UserHostAddress: {0}, username: {1}, params: {{{2}}}", hostAddress, username, @params);
} }
base.OnActionExecuting(filterContext); base.OnActionExecuting(ctx);
} }
} }
} }
@@ -1,5 +1,6 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Web.Mvc; using System.Web.Mvc;
using InventoryTraker.Web.Attributes;
using InventoryTraker.Web.Identity; using InventoryTraker.Web.Identity;
using InventoryTraker.Web.Models; using InventoryTraker.Web.Models;
using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity;
@@ -26,6 +27,7 @@ namespace InventoryTraker.Web.Controllers
} }
[HttpPost] [HttpPost]
[ActionLog]
public async Task<ActionResult> Login(LoginForm form) public async Task<ActionResult> Login(LoginForm form)
{ {
var user = await _userManager.FindByEmailAsync(form.EmailAddress); var user = await _userManager.FindByEmailAsync(form.EmailAddress);
@@ -43,6 +45,7 @@ namespace InventoryTraker.Web.Controllers
return Json(true); return Json(true);
} }
[ActionLog]
public ActionResult Logout() public ActionResult Logout()
{ {
_authManager.SignOut(); _authManager.SignOut();
@@ -1,18 +1,31 @@
using System.Collections.Generic; using System.Linq;
using System.Linq;
using System.Web.Mvc; using System.Web.Mvc;
using InventoryTraker.Web.ActionResults; using InventoryTraker.Web.ActionResults;
using InventoryTraker.Web.Utilities;
using NLog;
namespace InventoryTraker.Web.Controllers namespace InventoryTraker.Web.Controllers
{ {
public abstract class ControllerBase : Controller public abstract class ControllerBase : Controller
{ {
public BetterJsonResult<T> BetterJson<T>(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<T> BetterJson<T>(T model)
{ {
return new BetterJsonResult<T> {Data = model}; return new BetterJsonResult<T> {Data = model};
} }
protected string[] GetModelStateErrorList() private string[] GetModelStateErrorList()
{ {
var errorList = var errorList =
from kvp in ModelState from kvp in ModelState
@@ -4,6 +4,7 @@ using System.Linq;
using System.Web.Mvc; using System.Web.Mvc;
using AutoMapper; using AutoMapper;
using AutoMapper.QueryableExtensions; using AutoMapper.QueryableExtensions;
using InventoryTraker.Web.Attributes;
using InventoryTraker.Web.Core; using InventoryTraker.Web.Core;
using InventoryTraker.Web.Data; using InventoryTraker.Web.Data;
using InventoryTraker.Web.Models; using InventoryTraker.Web.Models;
@@ -49,6 +50,7 @@ namespace InventoryTraker.Web.Controllers
.OrderBy(x => x.InventoryType.Name); .OrderBy(x => x.InventoryType.Name);
} }
[ActionLog]
public JsonResult Add(InventoryAddForm form) public JsonResult Add(InventoryAddForm form)
{ {
if (!ModelState.IsValid) if (!ModelState.IsValid)
@@ -79,6 +81,7 @@ namespace InventoryTraker.Web.Controllers
return BetterJson(model); return BetterJson(model);
} }
[ActionLog]
public JsonResult Distribute(InventoryDistributeForm form) public JsonResult Distribute(InventoryDistributeForm form)
{ {
if (!ModelState.IsValid) if (!ModelState.IsValid)
@@ -136,6 +139,7 @@ namespace InventoryTraker.Web.Controllers
.ToArray()); .ToArray());
} }
[ActionLog]
public JsonResult Remove(InventoryRemoveForm form) public JsonResult Remove(InventoryRemoveForm form)
{ {
if (!ModelState.IsValid) if (!ModelState.IsValid)
@@ -1,8 +1,7 @@
using System; using System.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc; using System.Web.Mvc;
using AutoMapper.QueryableExtensions; using AutoMapper.QueryableExtensions;
using InventoryTraker.Web.Attributes;
using InventoryTraker.Web.Core; using InventoryTraker.Web.Core;
using InventoryTraker.Web.Data; using InventoryTraker.Web.Data;
using InventoryTraker.Web.Models; using InventoryTraker.Web.Models;
@@ -55,6 +54,7 @@ namespace InventoryTraker.Web.Controllers
return BetterJson(new { totalItems, transactions }); return BetterJson(new { totalItems, transactions });
} }
[ActionLog]
public JsonResult Delete(int transactionId) public JsonResult Delete(int transactionId)
{ {
var transaction = _context.Transactions.Find(transactionId); var transaction = _context.Transactions.Find(transactionId);
@@ -355,6 +355,7 @@
<Compile Include="Models\ProfileForm.cs" /> <Compile Include="Models\ProfileForm.cs" />
<Compile Include="Models\TransactionViewModel.cs" /> <Compile Include="Models\TransactionViewModel.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utilities\ControllerContextExtensions.cs" />
<Compile Include="Utilities\ExcelParserBase.cs" /> <Compile Include="Utilities\ExcelParserBase.cs" />
<Compile Include="Utilities\InventoryTypeParser.cs" /> <Compile Include="Utilities\InventoryTypeParser.cs" />
<Compile Include="Utilities\DateExtensions.cs" /> <Compile Include="Utilities\DateExtensions.cs" />
+4
View File
@@ -19,4 +19,8 @@
<target name="debugLogger" xsi:type="File" fileName="${basedir}/Logs/Debug.${shortdate}.txt" /> <target name="debugLogger" xsi:type="File" fileName="${basedir}/Logs/Debug.${shortdate}.txt" />
<target name="exceptionLogger" xsi:type="File" fileName="${basedir}/Logs/Error.${shortdate}.txt" /> <target name="exceptionLogger" xsi:type="File" fileName="${basedir}/Logs/Error.${shortdate}.txt" />
</targets> </targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="debugLogger" />
<logger name="*" minlevel="Error" writeTo="exceptionLogger" />
</rules>
</nlog> </nlog>
@@ -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}";
}
}
}
@@ -10,6 +10,7 @@ namespace InventoryTraker.Web.Utilities
{ {
private readonly FileSystemInfo _excelFile; private readonly FileSystemInfo _excelFile;
// ReSharper disable once ClassNeverInstantiated.Local
private sealed class InventoryTypeMap : CsvClassMap<InventoryType> private sealed class InventoryTypeMap : CsvClassMap<InventoryType>
{ {
public InventoryTypeMap() public InventoryTypeMap()