ASP.NET MVC

These steps describe how to install and configure KissLog for a ASP.NET MVC application.

A full working example can be found here.

By following the install instructions, you will will:

  • configure KissLog to capture and log all the unhandled exceptions

  • configure KissLog to capture all the HTTP properties (User-Agent, FormData, Headers, StatusCode, etc.)

  • register RequestLogsApiListener listener which will save the captured data to kisslog.net

Instructions

  1. Install NuGet Package

Package Manager Console
PM> Install-Package KissLog.AspNet.Mvc
  1. Update web.config

web.config
<configuration>
    <appSettings>
        <add key="KissLog.OrganizationId" value="_OrganizationId_" />
        <add key="KissLog.ApplicationId" value="_ApplicationId_" />
        <add key="KissLog.ApiUrl" value="https://api.kisslog.net" />
    </appSettings>
</configuration>
  1. Update Global.asax

Global.asax
 1using KissLog;
 2using KissLog.AspNet.Mvc;
 3using KissLog.AspNet.Web;
 4using KissLog.CloudListeners.Auth;
 5using KissLog.CloudListeners.RequestLogsListener;
 6
 7namespace AspNet.Mvc
 8{
 9    public class MvcApplication : System.Web.HttpApplication
10    {
11        protected void Application_Start()
12        {
13            AreaRegistration.RegisterAllAreas();
14            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
15            RouteConfig.RegisterRoutes(RouteTable.Routes);
16            BundleConfig.RegisterBundles(BundleTable.Bundles);
17
18            // Add KissLog exception filter
19            GlobalFilters.Filters.Add(new KissLogWebMvcExceptionFilterAttribute());
20
21            ConfigureKissLog();
22        }
23
24        protected void Application_Error(object sender, EventArgs e)
25        {
26            Exception exception = Server.GetLastError();
27            if (exception != null)
28            {
29                var logger = Logger.Factory.Get();
30                logger.Error(exception);
31
32                if (logger.AutoFlush() == false)
33                {
34                    Logger.NotifyListeners(logger);
35                }
36            }
37        }
38
39        private void ConfigureKissLog()
40        {
41            KissLogConfiguration.Listeners
42                .Add(new RequestLogsApiListener(new Application(ConfigurationManager.AppSettings["KissLog.OrganizationId"], ConfigurationManager.AppSettings["KissLog.ApplicationId"]))
43                {
44                    ApiUrl = ConfigurationManager.AppSettings["KissLog.ApiUrl"]
45                });
46        }
47
48        // Register HttpModule
49        public static KissLogHttpModule KissLogHttpModule = new KissLogHttpModule();
50
51        public override void Init()
52        {
53            base.Init();
54
55            KissLogHttpModule.Init(this);
56        }
57    }
58}
  1. Write logs using IKLogger

HomeController.cs
 1using KissLog;
 2using System.Web.Mvc;
 3
 4namespace AspNet.Mvc.Controllers
 5{
 6    public class HomeController : Controller
 7    {
 8        private readonly IKLogger _logger;
 9        public HomeController()
10        {
11            _logger = Logger.Factory.Get();
12        }
13
14        public ActionResult Index()
15        {
16            _logger.Trace("Trace message");
17            _logger.Debug("Debug message");
18            _logger.Info("Info message");
19
20            return View();
21        }
22    }
23}
ASP.NET MVC