Install AspNet WebApi

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

Steps

1. Install NuGet Package

Install NuGet package KissLog.AspNet.WebApi

PM> Install-Package KissLog.AspNet.WebApi

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.Web;
 
namespace KissLog.Samples.WebApi
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            GlobalConfiguration.Configure(WebApiConfig.Register);

            RegisterKissLogListeners();
        }

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

        private void RegisterKissLogListeners()
        {
            // cloud listener
            KissLogConfiguration.Listeners.Add(new KissLogApiListener(
                ConfigurationManager.AppSettings["KissLog.OrganizationId"],
                ConfigurationManager.AppSettings["KissLog.ApplicationId"],
                ConfigurationManager.AppSettings["KissLog.Environment"]
            ));
        }

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

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

            KissLogHttpModule.Init(this);
        }
    }
}

4. Update WebApiConfig.cs

using KissLog.AspNet.WebApi;
using System.Web.Http;
using System.Web.Http.ExceptionHandling;
 
namespace KissLog.Samples.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 }
            );
        }
    }
}

5. Access ILogger from ApiController

using KissLog;

namespace KissLog.Samples.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" };
        }
    }
}