Console App (.NET Core)¶
These steps describe how to install and configure KissLog for a .NET Core Console application.
A full working example can be found here.
By following the install instructions, you will:
create a “main” logger instance and use it throughout the
Main(string[] args)
method executionregister KissLog as
Microsoft.Extensions.Logging.ILogger<>
adapterregister
RequestLogsApiListener
listener which will save the captured data to kisslog.net
Instructions¶
Install NuGet Packages
Package Manager Console¶
PM> Install-Package KissLog.AspNetCore
Update appsettings.json
appsettings.json¶
{
"Logging": {
"LogLevel": {
"Default": "Trace",
"Microsoft": "Information"
}
},
"KissLog.OrganizationId": "_OrganizationId_",
"KissLog.ApplicationId": "_ApplicationId_",
"KissLog.ApiUrl": "https://api.kisslog.net"
}
Update Program.cs
Program.cs¶
1using KissLog;
2using KissLog.AspNetCore;
3using KissLog.CloudListeners.Auth;
4using KissLog.CloudListeners.RequestLogsListener;
5using KissLog.Formatters;
6
7namespace ConsoleApp_NetCore
8{
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 Logger.SetFactory(new KissLog.LoggerFactory(new Logger(url: "ConsoleApp/Main")));
14
15 IConfiguration configuration = new ConfigurationBuilder()
16 .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
17 .Build();
18
19 var serviceCollection = new ServiceCollection();
20 ConfigureServices(serviceCollection, configuration);
21 var serviceProvider = serviceCollection.BuildServiceProvider();
22
23 ConfigureKissLog(configuration);
24
25 ILogger logger = serviceProvider.GetService<ILogger<Program>>();
26
27 logger.LogTrace("Trace log");
28 logger.LogDebug("Debug log");
29 logger.LogInformation("Information log");
30
31 // notify the listeners
32 var loggers = Logger.Factory.GetAll();
33 Logger.NotifyListeners(loggers);
34 }
35
36 private static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
37 {
38 services.AddLogging(logging =>
39 {
40 logging
41 .AddConfiguration(configuration.GetSection("Logging"))
42 .AddKissLog(options =>
43 {
44 options.Formatter = (FormatterArgs args) =>
45 {
46 if (args.Exception == null)
47 return args.DefaultValue;
48
49 string exceptionStr = new ExceptionFormatter().Format(args.Exception, args.Logger);
50
51 return string.Join(Environment.NewLine, new[] { args.DefaultValue, exceptionStr });
52 };
53 });
54 });
55 }
56
57 private static void ConfigureKissLog(IConfiguration configuration)
58 {
59 KissLogConfiguration.InternalLog = (message) =>
60 {
61 Console.WriteLine(message);
62 };
63
64 KissLogConfiguration.Listeners
65 .Add(new RequestLogsApiListener(new Application(configuration["KissLog.OrganizationId"], configuration["KissLog.ApplicationId"]))
66 {
67 ApiUrl = configuration["KissLog.ApiUrl"],
68 UseAsync = false
69 });
70 }
71 }
72}
