.NET 6 Web App¶
These steps describe how to install and configure KissLog for a .NET 6 Web application.
A full working example can be found here.
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 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 | using KissLog.AspNetCore;
using KissLog.CloudListeners.Auth;
using KissLog.CloudListeners.RequestLogsListener;
using KissLog.Formatters;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddLogging(provider =>
{
provider
.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 });
};
});
});
builder.Services.AddHttpContextAccessor();
builder.Services.AddRazorPages();
builder.Services.AddControllers();
var app = builder.Build();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.UseKissLogMiddleware(options => {
options.Listeners.Add(new RequestLogsApiListener(new Application(
builder.Configuration["KissLog.OrganizationId"],
builder.Configuration["KissLog.ApplicationId"])
)
{
ApiUrl = builder.Configuration["KissLog.ApiUrl"]
});
});
app.Run();
|
Write logs:
HomeController.cs¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | using Microsoft.AspNetCore.Mvc;
namespace dotnet_WebApplication.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
_logger.LogDebug("Hello world from dotnet 6!");
return View();
}
}
}
|