AspNet MVC

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

1. Install NuGet Package

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

Replace OrganizationID and ApplicationID with Api Key values from the KissLog.net application configuration page.

web.config
1
2
3
4
5
6
<configuration>
    <appSettings>
        <add key="KissLog.OrganizationId" value="OrganizationID" />
        <add key="KissLog.ApplicationId" value="ApplicationID" />
    </appSettings>
</configuration>
  1. Update Global.asax

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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());

            ConfigureKissLog();
        }

        private void ConfigureKissLog()
        {
            // KissLog cloud listener
            KissLogConfiguration.Listeners.Add(new KissLogApiListener(new KissLog.Apis.v1.Auth.Application(
                ConfigurationManager.AppSettings["KissLog.OrganizationId"],
                ConfigurationManager.AppSettings["KissLog.ApplicationId"])
            ));
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            Exception exception = Server.GetLastError();
            if (exception != null)
            {
                var logger = Logger.Factory.Get();
                logger.Error(exception);

                if(logger.AutoFlush() == false)
                {
                    Logger.NotifyListeners(logger);
                }
            }
        }

        // Register HttpModule
        public static KissLogHttpModule KissLogHttpModule = new KissLogHttpModule();

        public override void Init()
        {
            base.Init();

            KissLogHttpModule.Init(this);
        }
    }
}
  1. Use the ILogger

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
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();
        }
    }
}

Testing

To test the KissLog configuration, trigger a request on /Home/Index. The request should be visible on KissLog.net.

Request details

Request details

Request logs

Request logs