Asp.net mvc 自定义数据库日志记录
我试图在数据库中存储自定义日志错误,所以Asp.net mvc 自定义数据库日志记录,asp.net-mvc,logging,Asp.net Mvc,Logging,我试图在数据库中存储自定义日志错误,所以 public enum Events { Error, Info, Debug } public class Logging { public int LoggingID { get; set; } public Events EventName { get; set; } public string Message { get; set; } public virtual Custom
public enum Events
{
Error,
Info,
Debug
}
public class Logging
{
public int LoggingID { get; set; }
public Events EventName { get; set; }
public string Message { get; set; }
public virtual Customer Customer { get; set; }
}
对于每一个事件,我都会发出这样的信息
LogHelper.Infos(" Running application ");
ConfigureAuth(app);
如何在数据库中存储这些事件?您可以这样定义日志实体:
public class LogAction
{
public DateTime PerformedAt { get; set; }
public string Controller { get; set; }
public string Action { get; set; }
public string Description { get; set; }
public string UserName { get; set; }
public string Ip { get; set; }
}
public class LogAttribute : ActionFilterAttribute
{
public string Description { get; set; }
public IUnitOfWork Uow { get; set; }
public ILogActionService LogActionService { get; set; }
public IApplicationUserManager ApplicationUserManager { get; set; }
public LogAttribute(string desciption)
{
Description = desciption;
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
string currentUserId = null;
if (filterContext.HttpContext.User.Identity.IsAuthenticated)
{
currentUserId = filterContext.HttpContext.User.Identity.GetUserId();
}
var model = new LogAction
{
UserName = currentUserId ?? "Anonymouse user",
Action = filterContext.ActionDescriptor.ActionName,
Controller = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName,
Description = Description,
PerformedAt = DateTime.Now,
Ip = Helper.Helper.GetUser_IP_Message()
};
LogActionService.AddLog(model);
Uow.SaveAllChanges();
base.OnActionExecuted(filterContext);
}
}
然后可以创建如下自定义属性:
public class LogAction
{
public DateTime PerformedAt { get; set; }
public string Controller { get; set; }
public string Action { get; set; }
public string Description { get; set; }
public string UserName { get; set; }
public string Ip { get; set; }
}
public class LogAttribute : ActionFilterAttribute
{
public string Description { get; set; }
public IUnitOfWork Uow { get; set; }
public ILogActionService LogActionService { get; set; }
public IApplicationUserManager ApplicationUserManager { get; set; }
public LogAttribute(string desciption)
{
Description = desciption;
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
string currentUserId = null;
if (filterContext.HttpContext.User.Identity.IsAuthenticated)
{
currentUserId = filterContext.HttpContext.User.Identity.GetUserId();
}
var model = new LogAction
{
UserName = currentUserId ?? "Anonymouse user",
Action = filterContext.ActionDescriptor.ActionName,
Controller = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName,
Description = Description,
PerformedAt = DateTime.Now,
Ip = Helper.Helper.GetUser_IP_Message()
};
LogActionService.AddLog(model);
Uow.SaveAllChanges();
base.OnActionExecuted(filterContext);
}
}
如您所见,在OnActionExecuted
方法中,您可以填充实体并将其保存到数据库中,因此您可以这样使用属性:
[Log("Home Page")]
public ActionResult Index()
{
return View();
}