Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 自定义数据库日志记录_Asp.net Mvc_Logging - Fatal编程技术网

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