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:
LocalTextFileListener
listener which will save the captured data to logs
folderPM> Install-Package KissLog.AspNet.WebApi
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); } } }
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