ASP.NET MVC 5 + Entity Framework 5 – Application Log Design Pattern

ASP.NET MVC 5 + Entity Framework 5 – Application Log Design Pattern

   Create SQL Table Statement
 
   CREATE TABLE [dbo].[EventLog] (
      [ID] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
      [ErrorCode] [numeric] NOT NULL DEFAULT(0),
      [ErrorTitle] [nvarchar](MAX) NOT NULL DEFAULT(''),
      [ErrorDesc] [nvarchar](MAX) NOT NULL DEFAULT(''),
      [ErrorDate] [datetime] NOT NULL,
      [ExecutionUser] [nvarchar](150) NOT NULL
   )
 
   EventLog.cs ( Project : InventoryDAO / Model Layer / generated from Entity Framework with Customization )
 
   namespace InventoryDAO
   {
      using System;
      using System.Collections.Generic;
 
      public partial class EventLog
      {
         public EventLog()
         {
         }
 
         public EventLog(decimal ErrorCode, string ErrorTitle, string ErrorDesc, DateTime ErrorDate, string ExecutionUser)
         {
            this.ErrorCode = ErrorCode;
            this.ErrorTitle = ErrorTitle;
            this.ErrorDesc = ErrorDesc;
            this.ErrorDate = ErrorDate;
            this.ExecutionUser = ExecutionUser;
         }
 
         public int ID { get; set; }
         public decimal ErrorCode { get; set; }
         public string ErrorTitle { get; set; }
         public string ErrorDesc { get; set; }
         public DateTime ErrorDate { get; set; }
         public string ExecutionUser { get; set; }
      }
   }
 
   DAO\Inventory\EventDAO.cs ( Project : InventoryDAO / Business Logic Layer )
 
   using System;
   using System.Linq;
 
   namespace InventoryDAO.DAO.Inventory
   {
      public class EventLogDAO
      {
         private InventoryEntities entity;
 
         public EventLogDAO()
         {
            this.entity = new InventoryEntities();
         }
 
         public void setEventLog(EventLog log)
         {
            this.entity.EventLogs.Add(log);
            this.entity.SaveChanges();
         }
      }
   }
 
   Controllers\InventoryController.cs ( Project : InventoryWeb / Controller Layer )
 
   using System;
   using System.Collections.Generic;
   using System.Web.Mvc;
 
   namespace InventoryWeb.Controllers
   {
      public class InventoryController : Controller
      {
         … …
 
         private InventoryDAO.DAO.Inventory.AuthDAO Auth;
         private InventoryDAO.DAO.Inventory.EventLogDAO Log;
         private string LoginName;
         private bool isAuth;
 
         public InventoryController()
         {
            … …
 
            this.Auth = new InventoryDAO.DAO.Inventory.AuthDAO();
            this.Log = new InventoryDAO.DAO.Inventory.EventLogDAO();
            this.LoginName = System.Web.HttpContext.Current.User.Identity.Name;
            this.isAuth = this.Auth.isAuth(this.LoginName);
         }
 
         public ActionResult Index()
         {
            if (this.isAuth)
               return View();
            else
            {
               this.Log.setEventLog(
                  new InventoryDAO.EventLog(
                     200,
                     "Authentication Error : Access Denied",
                     "Authentication Error : Access Denied",
                     System.DateTime.Now,
                     this.LoginName
                  )
               );
               return View("AccessDenied");
            }
         }
 
         public ActionResult AccessDenied()
         {
            return View();
         }
 
         … …
      }
   }