locked
How to insert error log in table in asp.net MVC5 using entity framework RRS feed

  • Question

  • User-471420332 posted

    Dear ALL,

    My application is an online and some time while uploading new patches getting error but i am not able to find what is that error in live website. So planing to add error log functionality and insert into table, because in older asp.net web forms, we can easy find event log in IIS but in this i am not able to finding, please suggest.

    Thank you

    Thursday, August 30, 2018 5:53 AM

All replies

  • User1724605321 posted

    Hi mazhar khan ,

    mazhar khan india

    So planing to add error log functionality and insert into table,

    You can design the log table using code first or  make use  of External library like Log4NetElmah  or create you own library class which will do logging to database . And write the logging logic in Application_Error event in Global.asax to handle the unhandled errors .

    Best Regards,

    Nan Yu

    Friday, August 31, 2018 3:13 AM
  • User1120430333 posted

    Dear ALL,

    My application is an online and some time while uploading new patches getting error but i am not able to find what is that error in live website. So planing to add error log functionality and insert into table, because in older asp.net web forms, we can easy find event log in IIS but in this i am not able to finding, please suggest.

    Thank you

    What you'll want is global exception handling where all unhandled exceptions are caught by the Basecontroller, that all controllers inherit,  and logged exception to a log file or to MS SQL server.  Log4Net can do both.

    https://www.code-sample.com/2014/08/base-controller-in-mvc-5.html

    using System;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using log4net;
    
    namespace MVC.Controllers
    {
        public abstract partial class BaseController : Controller
        {
            private ILog _logger;
    
            protected BaseController()
            {
                _logger =  LogManager.GetLogger(typeof(BaseController));
            }
    
            protected override void OnException(ExceptionContext filterContext)
            {
                AppException appException = new AppException(Convert.ToString(filterContext.Exception))
                {
                    Type = filterContext.GetType().ToString(),
                    StackTrace = filterContext.Exception.StackTrace,
                    Source = filterContext.Exception.Source,
                    InnerException = Convert.ToString(filterContext.Exception.InnerException)
                };
    
                _logger.Error(appException.ToString());
    
                Server.ClearError();
    
                RedirectToControllers("Home", "Error");
            }
    
            private void RedirectToControllers(string control, string action)
            {
                var routeData = new RouteData();
    
                routeData.Values["controller"] = control;
    
                routeData.Values["action"] = action;
    
                IController controller = new HomeController();
    
                ((IController) controller).Execute(new RequestContext(
                    new HttpContextWrapper(System.Web.HttpContext.Current), routeData));
            }
        }
    }
    using System.Web.Mvc;
    using Microsoft.AspNet.Identity;
    using MVC.Models;
    
    namespace MVC.Controllers
    {
        public class HomeController : BaseController
        {
            public ActionResult Index()
            {
                return View(new ProjectModels().GetProjectsByUserId(User.Identity.GetUserId()));
            }
    
            public ActionResult About()
            {
                ViewBag.Message = "Your application description page.";
    
                return View();
            }
    
            public ActionResult Contact()
            {
                ViewBag.Message = "Your contact page.";
    
                return View();
            }
    
            public ActionResult Error()
            {
                //  var feature = this.HttpContext.f.Features.Get<IExceptionHandlerFeature>();
                return View("~/Views/Shared/Error.cshtml", null);
            }
    
            public ActionResult Cancel()
            {
                return RedirectToAction("Index", "Home");
            }
    
            public ActionResult Task(int id)
            {
                return RedirectToAction("Index", "Task", new { id = id });
            }
        }
    }

    namespace MVC
    {
        using System;
    
        public class AppException : Exception
        {
            public AppException(String message) : base(message)
            { }
    
            public string Type { get; set; }
            public string StackTrace { get; set; }
            public string InnerException { get; set; }
            public AppException(String message, Exception inner) : base(message, inner) { }
        }
    
    }

    Friday, August 31, 2018 6:29 AM
  • User-35771671 posted

    +1 for ELMAH. ELMAH is exactly for tracking down errors, why I don't recommend you to write something custom. I wrote this ELMAH tutorial a few years ago: ELMAH Tutorial.

    Friday, August 31, 2018 11:00 AM
  • User475983607 posted

    Dear ALL,

    My application is an online and some time while uploading new patches getting error but i am not able to find what is that error in live website. So planing to add error log functionality and insert into table, because in older asp.net web forms, we can easy find event log in IIS but in this i am not able to finding, please suggest.

    Thank you

    Your question is very confusing... How do you know there is an error after deploying?  Can you describe the deployment process?  Once deployed, does the browser show an error?If so what is the error?  For example, a 500 or service not available?  Is this a logical error where the new deployment did not function as expected?  

    Lastly, the event log works the same in ASP.NET regardless of Web Forms or MVC.  If this is an ASP Core app then read the logging docs.

    Friday, August 31, 2018 11:20 AM
  • User-471420332 posted

    Lastly, the event log works the same in ASP.NET regardless of Web Forms or MVC

    Can you  explain me were the errors will be stored in MVC after deploying application in production server. We are deploying all files without publishing .

    In asp.net web forms as Application log in IIS  in Production server were we can get log were  and what is the  error exactly.But when we check for the MVC application  though we get error nothing recorded in Application log  in IIS

    Iam using VS 2013 MVC 5  code first approach.

    I tried with Elmah also but every time it shows no records web.config tags,tables,sps every thing verified but does not work.

    Please replay  

    Friday, September 7, 2018 1:34 PM
  • User475983607 posted

    There is simply not enough information in your post to guess where the issue is located, code, deployment, configuration, or simply misunderstanding basics.   I'm guessing the issue is related to the application restarting when the DLL is deployed.  I recommend meeting with your team and perhaps a senior developer to come up with a deployment strategy and discuss the current issues.  Also have another developer review the new code for correctness.

    Friday, September 7, 2018 2:33 PM