ASP.NET WebApi

These steps describe how to install and configure KissLog for a ASP.NET WebApi 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.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
 1using KissLog;
 2using KissLog.AspNet.Web;
 3using KissLog.CloudListeners.Auth;
 4using KissLog.CloudListeners.RequestLogsListener;
 5
 6namespace AspNet.WebApi
 7{
 8    public class WebApiApplication : System.Web.HttpApplication
 9    {
10        protected void Application_Start()
11        {
12            GlobalConfiguration.Configure(WebApiConfig.Register);
13
14            ConfigureKissLog();
15        }
16
17        protected void Application_Error(object sender, EventArgs e)
18        {
19            Exception exception = Server.GetLastError();
20            if (exception != null)
21            {
22                var logger = Logger.Factory.Get();
23                logger.Error(exception);
24
25                if(logger.AutoFlush() == false)
26                {
27                    Logger.NotifyListeners(logger);
28                }
29            }
30        }
31
32        private void ConfigureKissLog()
33        {
34            KissLogConfiguration.Listeners
35                .Add(new RequestLogsApiListener(new Application(ConfigurationManager.AppSettings["KissLog.OrganizationId"], ConfigurationManager.AppSettings["KissLog.ApplicationId"]))
36                {
37                    ApiUrl = ConfigurationManager.AppSettings["KissLog.ApiUrl"]
38                });
39        }
40
41        // Register HttpModule
42        public static KissLogHttpModule KissLogHttpModule = new KissLogHttpModule();
43
44        public override void Init()
45        {
46            base.Init();
47
48            KissLogHttpModule.Init(this);
49        }
50    }
51}
  1. Update WebApiConfig.cs

WebApiConfig.cs
 1using KissLog.AspNet.WebApi;
 2using System.Web.Http;
 3using System.Web.Http.ExceptionHandling;
 4
 5namespace AspNet.WebApi
 6{
 7    public static class WebApiConfig
 8    {
 9        public static void Register(HttpConfiguration config)
10        {
11            // Add KissLog Exception logger
12            config.Services.Replace(typeof(IExceptionLogger), new KissLogExceptionLogger());
13
14            // Add KissLog exception filter
15            config.Filters.Add(new KissLogWebApiExceptionFilterAttribute());
16
17            // Web API routes
18            config.MapHttpAttributeRoutes();
19
20            config.Routes.MapHttpRoute(
21                name: "DefaultApi",
22                routeTemplate: "api/{controller}/{id}",
23                defaults: new { id = RouteParameter.Optional }
24            );
25        }
26    }
27}
  1. Write logs using IKLogger

ValuesController.cs
 1using KissLog;
 2using System.Web.Http;
 3
 4namespace AspNet.WebApi.Controllers
 5{
 6    public class ValuesController : ApiController
 7    {
 8        private readonly IKLogger _logger;
 9        public ValuesController()
10        {
11            _logger = Logger.Factory.Get();
12        }
13
14        // GET api/values
15        public IEnumerable<string> Get()
16        {
17            _logger.Trace("Trace message");
18            _logger.Debug("Debug message");
19            _logger.Info("Info message");
20
21            return new string[] { "value1", "value2" };
22        }
23    }
24}
ASP.NET WebApi