locked
log4net + Hangfire ASP.net MVC RRS feed

  • Question

  • User-886811842 posted

    Hi Guys,
     
       Please let me know how to implement log4net  +  hangfire inside the ASP.net MVC projects ,if you have idea , code or reference it will be very help full for us   

    Wednesday, March 21, 2018 10:21 AM

All replies

  • User1120430333 posted

    Here is example of Log4net being used in the Basecontoller that all controllers in the solution inherit from to do global exception handling. the implementations of Lo4net  was taken from an ASP.NET Log4net tutorial found by using Google.

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

    Wednesday, March 21, 2018 11:00 AM
  • User-886811842 posted

    Thanks for your reply with log4net ,
    please let me know how i can implement with Hangfire framework inside it please let me know if you have any idea .

    Thursday, March 22, 2018 5:28 AM
  • User1120430333 posted

    Thanks for your reply with log4net ,
    please let me know how i can implement with Hangfire framework inside it please let me know if you have any idea .

    I don't know, because I have never used it. 

    Thursday, March 22, 2018 8:25 AM
  • User61956409 posted

    Hi sujilkumar,

    Hangfire is an open-source framework that can help perform background processing in .NET and .NET Core applications. If you’d like to integrate Hangfire with your MVC application, you can refer to this tutorial that explained how to send Mail in background via  Hangfire with ASP.NET MVC.

    With Regards,

    Fei Han

    Sunday, March 25, 2018 5:35 AM