-
Notifications
You must be signed in to change notification settings - Fork 3
ASP.NET WebApi
Catalin Gavan edited this page Oct 15, 2024
·
7 revisions
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:
- configure KissLog to capture and log all the unhandled exceptions
- configure KissLog to capture all the HTTP properties (User-Agent, QueryString, FormData, Headers, StatusCode, etc.)
- register
LocalTextFileListenerlistener which will save the captured data tologsfolder
- Install NuGet Package
PM> Install-Package KissLog.AspNet.WebApi
- 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);
}
}
}- 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" };
}
}
}