.NET Serilog Configuration

Ship logs from ASP.NET to Logit.io with Serilog

Use Serilog to send .NET logs to your Logit.io Stack.

Install Integration

Please click on the Install Integration button to configure your stack for this source.

Install

If using Visual Studio, create a new application using the ASP.NET Core Web App (Model-View-Controller) template.

If using a terminal application e.g. Powershell run the following command (where 'SerilogTestApp' is the name of the application you want to create):

dotnet new mvc -n SerilogTestApp

You will need to use NuGet or the command line to install three Serilog packages: Serilog.AspNetCore and Serilog.Sinks.Network to send logs using TCP/TLS or UDP.

Serilog.Settings.Configuration is needed to setup Serilog using settings from appsettings.json (instead of hardcoded settings).

In Powershell:

dotnet add package Serilog.AspNetCore
dotnet add package Serilog.Settings.Configuration
dotnet add package Serilog.Sinks.Network

If you are using Visual Studio, navigate to Tools -> NuGet Package Manager -> Manage NuGet packages for solution, browse for the 3 packages mentioned above and install them in your new application.

Configuring Serilog

⚠️

No TCP-SSL input available! Your stack is missing the required input for this data source.

You can send over TCP and UDP but this is not recommended.Talk to support to add the input

Using Serilog

Inside Program.cs, add Serilog to the WebApplication build process. Below is the updated Program.cs code in full:

using Serilog;
 
var builder = WebApplication.CreateBuilder(args);
 
builder.Host.UseSerilog((hostContext, _, configuration) =>
{
    configuration.ReadFrom.Configuration(hostContext.Configuration);
});
 
// Add services to the container.
builder.Services.AddControllersWithViews();
 
var app = builder.Build();
 
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}
 
app.UseHttpsRedirection();
app.UseStaticFiles();
 
app.UseRouting();
 
app.UseAuthorization();
 
app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
 
app.Run();

You can now log from anywhere in your application. For example, the following code will send a warning log to your stack when the application hits the Index action of the Home controller:

using Microsoft.AspNetCore.Mvc;
using SerilogTestApp.Models;
using System.Diagnostics;
 
namespace SerilogTestApp.Controllers
{
    public class HomeController(ILogger<HomeController> logger) : Controller
    {
        public IActionResult Index()
        {
            logger.LogWarning("This is something you need to be warned about!");
            return View();
        }
 
        public IActionResult Privacy()
        {
            return View();
        }
 
        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}

Launch Logs to View Data

Run the application and click on the button below to open the log management visualizer:

Launch Logs

You should now see the above log stored in your stack.

Serilog is a fully featured logging framework and has many other capabilities that are not detailed here.

Please see the Serilog.AspNetCore Documentation (opens in a new tab) for more information.

If you need any help with migrating your .NET Core logs to Logit.io we are here to help. Feel free to get in touch with our support team via Intercom and we will be happy to assist.