Dependency Injection

Integrating KissLog with Dependency Injection frameworks is a simple process.

When we register the ILogger, we simply instruct the IoC container to execute the Logger.Factory.Get() method anytime it is requested.

Calling Logger.Factory.Get() multiple times will return the same ILogger instance per Http Request.

Examples

.NET Core

For .NET Core appplications, ILogger is already registered on the DI Container as part of Install process.

Implementation can be found on Startup.cs

using KissLog;

namespace KissLog.Samples.AspNetCore
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddScoped<ILogger>((context) =>
            {
                return Logger.Factory.Get();
            });
        }
    }
}

Unity

Update UnityContainer

using KissLog;

namespace MyApplication
{
    public static class UnityConfig
    {
        private static void ConfigureContainer()
        {
            var container = new UnityContainer();

            container.RegisterType<ILogger>(
                new InjectionFactory(u => Logger.Factory.Get())
            );
        }
    }
}

Ninject

Update NinjectWebCommon.cs

using KissLog;

namespace MyApplication.App_Start
{
    public static class NinjectWebCommon 
    {
        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<ILogger>().ToMethod((context) =>
            {
                return Logger.Factory.Get();
            });
        }
    }
}

Autofac

Update ContainerBuilder

using KissLog;

namespace MyApplication
{
    public static class AutofacConfig
    {
        private static void ConfigureContainer()
        {
            var builder = new ContainerBuilder();

            builder
                .Register(p => Logger.Factory.Get())
                .As<ILogger>();
        }
    }
}