.NET Core Web App¶
These steps describe how to install and configure KissLog for a .NET Core Web Application.
A full working example can be found here.
By following the install instructions, you will will:
register KissLog as
Microsoft.Extensions.Logging.ILogger<>
adapterconfigure KissLog to capture and log all the unhandled exceptions
configure KissLog to capture all the HTTP properties (User-Agent, FormData, Headers, StatusCode, etc.)
register
RequestLogsApiListener
listener which will save the captured data to kisslog.net
Instructions¶
Install NuGet Package
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 Startup.cs
Startup.cs¶
1using KissLog;
2using KissLog.AspNetCore;
3using KissLog.CloudListeners.Auth;
4using KissLog.CloudListeners.RequestLogsListener;
5using KissLog.Formatters;
6
7namespace AspNetCore_WebApp
8{
9 public class Startup
10 {
11 public Startup(IConfiguration configuration)
12 {
13 Configuration = configuration;
14 }
15
16 public IConfiguration Configuration { get; }
17
18 public void ConfigureServices(IServiceCollection services)
19 {
20 services.AddHttpContextAccessor();
21
22 // Optional. Register IKLogger if you use KissLog.IKLogger instead of Microsoft.Extensions.Logging.ILogger<>
23 services.AddScoped<IKLogger>((provider) => Logger.Factory.Get());
24
25 services.AddLogging(logging =>
26 {
27 logging.AddKissLog(options =>
28 {
29 options.Formatter = (FormatterArgs args) =>
30 {
31 if (args.Exception == null)
32 return args.DefaultValue;
33
34 string exceptionStr = new ExceptionFormatter().Format(args.Exception, args.Logger);
35
36 return string.Join(Environment.NewLine, new[] { args.DefaultValue, exceptionStr });
37 };
38 });
39 });
40
41 services.AddControllersWithViews();
42 }
43
44 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
45 {
46 if (env.IsDevelopment())
47 {
48 app.UseDeveloperExceptionPage();
49 }
50 else
51 {
52 app.UseExceptionHandler("/Home/Error");
53 }
54
55 app.UseStaticFiles();
56 app.UseRouting();
57 app.UseAuthorization();
58 app.UseSession();
59
60 app.UseKissLogMiddleware(options => ConfigureKissLog(options));
61
62 app.UseEndpoints(endpoints =>
63 {
64 endpoints.MapControllerRoute(
65 name: "default",
66 pattern: "{controller=Home}/{action=Index}/{id?}");
67 });
68 }
69
70 private void ConfigureKissLog(IOptionsBuilder options)
71 {
72 KissLogConfiguration.Listeners
73 .Add(new RequestLogsApiListener(new Application(configuration["KissLog.OrganizationId"], configuration["KissLog.ApplicationId"]))
74 {
75 ApiUrl = configuration["KissLog.ApiUrl"]
76 });
77 }
78 }
79}
Write logs:
HomeController.cs¶
1using Microsoft.Extensions.Logging;
2
3namespace AspNetCore_WebApp.Controllers
4{
5 public class HomeController : Controller
6 {
7 private readonly ILogger<HomeController> _logger;
8 public HomeController(ILogger<HomeController> logger)
9 {
10 _logger = logger;
11 }
12
13 public IActionResult Index()
14 {
15 _logger.LogTrace("Trace log");
16 _logger.LogDebug("Debug log");
17 _logger.LogInformation("Information log");
18
19 return View();
20 }
21 }
22}
