Usage for WindowsApps

For Windows / Console applications, we need to create and flush the ILogger manually.

This can be achieved by using a try-catch-finally block.

class Program
{
    static void Main(string[] args)
    {
        // create the logger
        ILogger logger = new Logger(url: "Main");

        try
        {
            // execute main
        }
        catch(Exception ex)
        {
            logger.Error(ex);
            throw;
        }
        finally
        {
            // flush the logger
            Logger.NotifyListeners(logger);
        }
    }
}

The captured log messages will be persisted only when the Logger.NotifyListeners(logger) method is invoked.

Acquire logger instance

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

The url parameter is used to identify the method which is being logged.

Each different method should create and flush a different instance of ILogger.

class Program
{
    static void Foo()
    {
        ILogger logger = new Logger(url: "Foo");

        // foo

        Logger.NotifyListeners(logger);
    }

    static void Bar()
    {
        ILogger logger = new Logger(url: "Bar");

        // bar

        Logger.NotifyListeners(logger);
    }
}

Example

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

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

            try
            {
                logger.Debug("Hello world from Console application!");
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                throw;
            }
            finally
            {
                Logger.NotifyListeners(logger);
            }
        }

        static void ConfigureKissLog()
        {
            KissLogConfiguration.Listeners.Add(new KissLogApiListener(new KissLog.Apis.v1.Auth.Application(
                "0337cd29-a56e-42c1-a48a-e900f3116aa8",
                "c49f1fa1-00b8-4a43-8bc6-b327c08fb229")
            )
            {
                UseAsync = false
            });
        }
    }
}

Console app logs