A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/catalingavan/KissLog.Sdk/wiki/ASP.NET-WebApi below:

ASP.NET WebApi · catalingavan/KissLog.Sdk Wiki · GitHub

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:

  1. Install NuGet Package
PM> Install-Package KissLog.AspNet.WebApi
  1. Update Global.asax
using KissLog;
using KissLog.AspNet.Web;
using KissLog.CloudListeners.Auth;
using KissLog.CloudListeners.RequestLogsListener;
using System;
using System.Web.Http;

namespace KissLogExample.AspNet.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()
        {
            KissLogConfiguration.Listeners.Add(new RequestLogsApiListener(new Application(
                "_OrganizationId_",     // Get from Application configuration page
                "_ApplicationId_"
            ))
            {
                ApiUrl = "https://api.logbee.net/"
            });
        }

        public static KissLogHttpModule KissLogHttpModule = new KissLogHttpModule();

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

            KissLogHttpModule.Init(this);
        }
    }
}
  1. Update WebApiConfig.cs
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 }
            );
        }
    }
}

Logs can be written using KissLog.IKLogger interface.

To receive the logger instance, use Logger.Factory.Get() factory method.

For web applications, Logger.Factory.Get() will return the same logger instance shared across the HTTP request.

using KissLog;
using System.Collections.Generic;
using System.Web.Http;
 
namespace MyApp.WebApi.Controllers
{
    public class ValuesController : ApiController
    {
        private readonly IKLogger _logger;
        public ValuesController()
        {
            _logger = Logger.Factory.Get();
        }
 
        // GET api/values
        public IEnumerable Get()
        {
            _logger.Debug("Hello world from ASP.NET WebApi!");
            return new string[] { "value1", "value2" };
        }
    }
}

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4