-
Notifications
You must be signed in to change notification settings - Fork 3
dotnet WebApp
Catalin Gavan edited this page Oct 15, 2024
·
1 revision
These steps describe how to install and configure KissLog for a .NET Web Application.
A full working example can be found here.
- Install NuGet Package
PM> Install-Package KissLog.AspNetCore
- Update Startup.cs
using KissLog.AspNetCore;
using KissLog.CloudListeners.Auth;
using KissLog.CloudListeners.RequestLogsListener;
using KissLog.Formatters;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpContextAccessor();
builder.Services.AddControllersWithViews();
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 });
};
});
});
var app = builder.Build();
app.MapGet("/", (ILogger<Program> logger) =>
{
logger.LogInformation("Hello from minimal API");
return "Hello World!";
});
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.UseKissLogMiddleware(options => {
options.Listeners.Add(new RequestLogsApiListener(new Application(
"_OrganizationId_", // Get from Application configuration page
"_ApplicationId_"
))
{
ApiUrl = "https://api.logbee.net/"
});
});
app.Run();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!");
return View();
}
}
}