Install Net Core

These steps describe how to install and configure KissLog for a .NET Core application.

Steps

1. Install NuGet Package

Install NuGet package KissLog.AspNetCore

PM> Install-Package KissLog.AspNetCore

2. Update appSettings.json

Replace Organization ID and Application ID with corresponding values from Application Settings page.

{
    "KissLog.OrganizationId": "Organization ID",
    "KissLog.ApplicationId": "Application ID",
    "KissLog.Environment": null
}

3. Update Startup.cs

using KissLog.Apis.v1.Listeners;
using KissLog.AspNetCore;
using KissLog.Listeners;
 
namespace KissLog.Samples.AspNetCore
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            // register dependencies
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            services.AddScoped<ILogger>((context) =>
            {
                return Logger.Factory.Get();
            });

            services.AddSession();

            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseStaticFiles();

            app.UseSession();

            // make sure it is added after app.UseStaticFiles() and app.UseSession(),
            // and before app.UseMvc()
            app.UseKissLogMiddleware();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

            RegisterKissLogListeners();
        }

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

4. Access ILogger from HomeController

using KissLog;

namespace KissLog.Samples.AspNetCore.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger _logger;
        public HomeController(ILogger logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            _logger.Debug("Hello world from AspNetCore!");

            return View();
        }
    }
}