.NET Core Console App

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

A full working example can be found here.

Instructions

  1. Install NuGet Packages

Package Manager Console
PM> Install-Package KissLog.AspNetCore
  1. Update appsettings.json

appsettings.json
{
    "Logging": {
        "LogLevel": {
            "Default": "Trace",
            "Microsoft": "Information"
        }
    },

    "KissLog.OrganizationId": "_OrganizationId_",
    "KissLog.ApplicationId": "_ApplicationId_",
    "KissLog.ApiUrl": "https://api.kisslog.net"
}
  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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using KissLog;
using KissLog.AspNetCore;
using KissLog.CloudListeners.Auth;
using KissLog.CloudListeners.RequestLogsListener;
using KissLog.Formatters;

namespace ConsoleApp_NetCore
{
    class Program
    {
        static void Main(string[] args)
        {
            Logger.SetFactory(new KissLog.LoggerFactory(new Logger(url: "ConsoleApp/Main")));

            IConfiguration configuration = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .Build();

            var serviceCollection = new ServiceCollection();
            ConfigureServices(serviceCollection, configuration);
            var serviceProvider = serviceCollection.BuildServiceProvider();

            ConfigureKissLog(configuration);

            ILogger logger = serviceProvider.GetService<ILogger<Program>>();

            logger.LogTrace("Trace log");
            logger.LogDebug("Debug log");
            logger.LogInformation("Information log");

            // notify the listeners
            var loggers = Logger.Factory.GetAll();
            Logger.NotifyListeners(loggers);
        }

        private static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            services.AddLogging(logging =>
            {
                logging
                    .AddConfiguration(configuration.GetSection("Logging"))
                    .AddKissLog(options =>
                    {
                        options.Formatter = (FormatterArgs args) =>
                        {
                            if (args.Exception == null)
                                return args.DefaultValue;

                            string exceptionStr = new ExceptionFormatter().Format(args.Exception, args.Logger);

                            return string.Join(Environment.NewLine, new[] { args.DefaultValue, exceptionStr });
                        };
                    });
            });
        }

        private static void ConfigureKissLog(IConfiguration configuration)
        {
            KissLogConfiguration.InternalLog = (message) =>
            {
                Console.WriteLine(message);
            };

            KissLogConfiguration.Listeners
                .Add(new RequestLogsApiListener(new Application(configuration["KissLog.OrganizationId"], configuration["KissLog.ApplicationId"]))
                {
                    ApiUrl = configuration["KissLog.ApiUrl"],
                    UseAsync = false
                });
        }
    }
}
Console App (.NET Core)