Dependency injection

Integrating KissLog with a dependency injection framework is a simple process.

When ask for ILogger, resolve it at runtime with Logger.Factory.Get() factory method.

Non-web applications

For non-web applications, do not inject the ILogger with IoC container.

Instead, the ILogger should be created and flushed manually. Check the Console applications example.

.NET Core

For .NET Core appplications, ILogger is already registered as part of the install instructions.

Startup.cs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
namespace KissLog.Samples.AspNetCore
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddScoped<ILogger>((context) =>
            {
                return Logger.Factory.Get();
            });
        }
    }
}

Unity

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
namespace MyApplication
{
    public static class UnityConfig
    {
        private static void ConfigureContainer()
        {
            var container = new UnityContainer();

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

Ninject

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
namespace MyApplication.App_Start
{
    public static class NinjectWebCommon
    {
        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<ILogger>().ToMethod((context) =>
            {
                return Logger.Factory.Get();
            });
        }
    }
}

Autofac

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
namespace MyApplication
{
    public static class AutofacConfig
    {
        private static void ConfigureContainer()
        {
            var builder = new ContainerBuilder();

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