Console applications

For Console applications, you need to create and flush the ILogger manually.

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
static void Main(string[] args)
{
    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);
    }
}

We call Logger.NotifyListeners(logger) (line 19) to ensure that the listeners will execute the OnFlush() event.

Listeners events

The log listeners are notified as following:

  • OnBeginRequest() is executed automatically when the Logger instance is created (line 3)

  • OnMessage() is executed automatically for each log message created (lines 7, 13)

  • OnFlush() is executed manually by calling Logger.NotifyListeners() (line 18)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
static void Main(string[] args)
{
    ILogger logger = new Logger(url: "Main");   // <-- OnBeginRequest()

    try
    {
        logger.Info("Executing main");          // <-- OnMessage()

        // execute Main
    }
    catch(Exception ex)
    {
        logger.Error(ex);                       // <-- OnMessage()
        throw;
    }
    finally
    {
        Logger.NotifyListeners(logger);         // <-- OnFlush()
    }
}

Use separate instances

ILogger has a scoped lifetime. In Console applications, the scope is defined by the method itself.

If you have different entry methods, you need to create and flush the logger for each of them.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Program
{
    static void SyncDatabase()
    {
        ILogger logger = new Logger(url: "SyncDatabase");

        // executing
    }

    static void ImportFromXml(string xmlPath)
    {
        ILogger logger = new Logger(url: "ImportFromXml");

        // executing
    }
}

Full example

In the following example we have a Console application which is saving the logs to KissLog.net.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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()
        {
            string organizationId = "0337cd29-a56e-42c1-a48a-e900f3116aa8";
            string applicationId = "c49f1fa1-00b8-4a43-8bc6-b327c08fb229";

            ILogListener listener = new KissLogApiListener(new KissLog.Apis.v1.Auth.Application(organizationId,applicationId))
            {
                UseAsync = false
            };

            KissLogConfiguration.Listeners.Add(listener);
        }
    }
}
Console application

Console app Main() on KissLog.net