Create ILogger instance

ILogger has a scoped lifetime.

It should be created at the beginning of a method, and flushed at the end of the methods execution.

Web applications

ILogger logger = Logger.Factory.Get();

For web applications, the ILogger is created, shared and flushed automatically for each http request (connection).

To retrieve the current ILogger instance, use the Logger.Factory.Get() factory method.

Example:
public class HomeController : Controller
{
    private readonly ILogger _logger;
    public HomeController()
    {
        _logger = Logger.Factory.Get();
    }

    public IActionResult Index()
    {
        _logger.Debug("Hello World!");

        return View();
    }
}

Calling the Logger.Factory.Get() method multiple times will return the same instance of ILogger.

public class HomeController : Controller
{
    public IActionResult Index()
    {
        for(int i = 1; i <= 5; i++)
        {
            ILogger logger = Logger.Factory.Get();
            logger.Info("Hello " + i);
        }

        int numberOfLogs = (Logger(Logger.Factory.Get())).DataContainer.LogMessages.Count();
        // get the number of log messages created for the current http request
        // numberOfLogs == 5

        return View();
    }
}

Console applications

ILogger logger = new Logger(url: "Main");

For console applications, the ILogger needs to be created and flushed manually.

This can be achieved by using a try-catch-finally block, which simulates the BEGIN and the END of a method.

class Program
{
    static void Main(string[] args)
    {
        ConfigureKissLog();

        ILogger logger = new Logger(url: "Main");

        try
        {
            logger.Info("Executing main");

            // execute Main
        }
        catch(Exception ex)
        {
            logger.Error(ex);
            throw;
        }
        finally
        {
            // notify the listeners
            Logger.NotifyListeners(logger);
        }
    }
}

The optional url: "Main" argument simulates an Uri identifier for the method which is being executed.

We flush the logs in the finally block by executing Logger.NotifyListeners() method.