Console applications

These steps describe how to install and configure KissLog for a Console application.

  1. Install NuGet Packages

Package Manager Console
PM> Install-Package KissLog
PM> Install-Package KissLog.Apis.v1
  1. Update App.config

App.config
<configuration>
    <appSettings>
        <add key="KissLog.OrganizationId" value="_OrganizationId_" />
        <add key="KissLog.ApplicationId" value="_ApplicationId_" />
        <add key="KissLog.ApiUrl" value="https://api.kisslog.net" />
    </appSettings>
</configuration>
  1. Update Program.cs

Program.cs
 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using KissLog;
using KissLog.Apis.v1.Listeners;

namespace MyApp.ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            ConfigureKissLog();

            // [...]
        }

        static void ConfigureKissLog()
        {
            // optional KissLog configuration
            KissLogConfiguration.Options
                .AppendExceptionDetails((Exception ex) =>
                {
                    StringBuilder sb = new StringBuilder();

                    if (ex is DivideByZeroException divideByZeroException)
                    {
                        sb.AppendLine("DivideByZeroException");
                    }

                    return sb.ToString();
                });

            // KissLog internal logs
            KissLogConfiguration.InternalLog = (message) =>
            {
                Debug.WriteLine(message);
            };

            // register logs output
            RegisterKissLogListeners();
        }

        static void RegisterKissLogListeners()
        {
            // multiple listeners can be registered using KissLogConfiguration.Listeners.Add() method

            // add KissLog.net cloud listener
             KissLogConfiguration.Listeners.Add(new KissLogApiListener(new KissLog.Apis.v1.Auth.Application(
                ConfigurationManager.AppSettings["KissLog.OrganizationId"],
                ConfigurationManager.AppSettings["KissLog.ApplicationId"])
            )
            {
                ApiUrl = ConfigurationManager.AppSettings["KissLog.ApiUrl"],
                UseAsync = false
            });
        }
    }
}
  1. Write logs using ILogger

Program.cs
 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
40
41
42
43
44
45
46
47
48
namespace MyApp.ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            ConfigureKissLog();

            Foo();
        }

        private static void Foo()
        {
            // create the ILogger
            ILogger logger = new Logger(url: "/Foo");

            try
            {
                logger.Info("Foo started");

                // execute foo
            }
            catch (Exception ex)
            {
                // capture and log exceptions
                logger.Error(ex);
                throw;
            }
            finally
            {
                logger.Info("Foo completed");

                // notify the listeners
                Logger.NotifyListeners(logger);
            }
        }

        static void ConfigureKissLog()
        {
            // [...]
        }

        static void RegisterKissLogListeners()
        {
            // [...]
        }
    }
}