ASP.NET WebApi

These steps describe how to install and configure KissLog for an ASP.NET WebApi application.

  1. Install NuGet Package

Package Manager Console
PM> Install-Package KissLog.AspNet.WebApi
  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
 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using KissLog;
using KissLog.AspNet.Web;
using KissLog.CloudListeners.Auth;
using KissLog.CloudListeners.RequestLogsListener;

namespace MyApp.WebApi
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            GlobalConfiguration.Configure(WebApiConfig.Register);

            ConfigureKissLog();
        }

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

        private void ConfigureKissLog()
        {
            // optional KissLog configuration
            KissLogConfiguration.Options
                .AppendExceptionDetails((Exception ex) =>
                {
                    StringBuilder sb = new StringBuilder();

                    if (ex is System.NullReferenceException nullRefException)
                    {
                        sb.AppendLine("Important: check for null references");
                    }

                    return sb.ToString();
                });

            // KissLog internal logs
            KissLogConfiguration.InternalLog = (message) =>
            {
                Debug.WriteLine(message);
            };

            // register logs output
            RegisterKissLogListeners();
        }

        private void RegisterKissLogListeners()
        {
            // multiple listeners can be registered using KissLogConfiguration.Listeners.Add() method

            // add KissLog.net cloud listener
            KissLogConfiguration.Listeners.Add(new RequestLogsApiListener(new Application(
                ConfigurationManager.AppSettings["KissLog.OrganizationId"],
                ConfigurationManager.AppSettings["KissLog.ApplicationId"])
            )
            {
                ApiUrl = ConfigurationManager.AppSettings["KissLog.ApiUrl"]
            });
        }

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

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

            KissLogHttpModule.Init(this);
        }
    }
}
  1. Update WebApiConfig.cs

WebApiConfig.cs
 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
using KissLog.AspNet.WebApi;
using System.Web.Http;
using System.Web.Http.ExceptionHandling;

namespace MyApp.WebApi
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Add KissLog Exception logger
            config.Services.Replace(typeof(IExceptionLogger), new KissLogExceptionLogger());

            // Add KissLog exception filter
            config.Filters.Add(new KissLogWebApiExceptionFilterAttribute());

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}
  1. Write logs using ILogger

ValuesController.cs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
using KissLog;
using System.Web.Http;

namespace MyApp.WebApi.Controllers
{
    public class ValuesController : ApiController
    {
        private readonly ILogger _logger;
        public ValuesController()
        {
            _logger = Logger.Factory.Get();
        }

        // GET api/values
        public IEnumerable<string> Get()
        {
            _logger.Debug("Hello world from AspNet.WebApi!");

            return new string[] { "value1", "value2" };
        }
    }
}