Install AspNet Mvc

These steps describe how to install and configure KissLog for an Asp.Net MVC application.

Steps

1. Install NuGet Package

Install NuGet package KissLog.AspNet.Mvc

PM> Install-Package KissLog.AspNet.Mvc

2. Update web.config

Replace Organization ID and Application ID with corresponding values from Application Settings page.

<configuration>
  <appSettings>
    <add key="KissLog.OrganizationId" value="Organization ID" />
    <add key="KissLog.ApplicationId" value="Application ID" />
    <add key="KissLog.Environment" value="" />
  </appSettings>
</configuration>

3. Update Global.asax

using KissLog.Apis.v1.Listeners;
using KissLog.AspNet.Mvc;
using KissLog.AspNet.Web;
 
namespace KissLog.Samples.AspNetMvc
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
 
            // Add KissLog exception filter
            GlobalFilters.Filters.Add(new KissLogWebMvcExceptionFilterAttribute());
 
            RegisterKissLogListeners();
        }
 
        private void RegisterKissLogListeners()
        {
            // cloud listener
            KissLogConfiguration.Listeners.Add(new KissLogApiListener(
                ConfigurationManager.AppSettings["KissLog.OrganizationId"],
                ConfigurationManager.AppSettings["KissLog.ApplicationId"],
                ConfigurationManager.AppSettings["KissLog.Environment"]
            ));
        }
 
        protected void Application_Error(object sender, EventArgs e)
        {
            Exception exception = Server.GetLastError();
            if (exception != null)
            {
                var logger = Logger.Factory.Get();
                logger.Error(exception);
            }
        }
 
        // Register HttpModule
        public static KissLogHttpModule KissLogHttpModule = new KissLogHttpModule();
 
        public override void Init()
        {
            base.Init();
 
            KissLogHttpModule.Init(this);
        }
    }
}

4. Access ILogger from HomeController

using KissLog;

namespace KissLog.Samples.AspNetMvc.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger _logger;
        public HomeController()
        {
            _logger = Logger.Factory.Get();
        }
 
        public ActionResult Index()
        {
            _logger.Debug("Hello world from AspNet.Mvc!");
 
            return View();
        }
    }
}