Usage

KissLog NuGet package represents the core library used for logging.

ILogger represents the principal component used to write log messages.

Basic usage

using KissLog;

public class Service
{
    private readonly ILogger _logger;
    public Service()
    {
        // acquire logger instance
        _logger = Logger.Factory.Get();
    }

    public void Foo(string productId, double price)
    {
        // write log messages
        logger.Debug($"Foo begin with args: {productId}, {price}");
    }
}

Acquire logger instance

ILogger logger = Logger.Factory.Get([string categoryName = "Default"]);

ILogger instance is acquired by using the Logger.Factory.Get() factory method.

An ILogger instance is created automatically and shared for each HTTP request.

As result, calling Logger.Factory.Get() multiple times will return the same ILogger instance.

using KissLog;

public class Service
{
    private readonly ILogger _logger;
    public Service()
    {
        _logger = Logger.Factory.Get();
    }
}

Log files

You can log files in a similar way you log messages.

When a file is logged, a temporary copy is created. The original file is not altered or referenced, and it can be safely manipulated afterwards.

using KissLog;

public class Service
{
    private readonly ILogger _logger;
    public Service()
    {
        _logger = Logger.Factory.Get();
    }

    public void Foo()
    {
        byte[] archive = File.ReadAllBytes(@"C:\Files\bootstrap.zip");
        _logger.LogAsFile(archive, "Bootstrap.zip");

        string path = @"C:\Files\Invoice-16-11-2017.pdf";
        _logger.LogFile(path, "Invoice.pdf");
    }
}

How it works

ILogger saves the logs in memory.

Only at the end of the HTTP request, the messages will be persisted to the output locations.

As a result, when saving the logs, the ILogListener will have access to all the HTTP properties including: Url, StatusCode, FormData, QueryString etc.