First Look at Logging With the .NET 7

Note: As of the release of this article, .NET 7 has fully released. The contents of this article still apply, but you can learn more about Loupe on .NET 7 here.

Last month, the final .NET 7 preview was released, the next version of .NET Core. It is considered a “current release” with support from this November to May of 2024. It has new features like faster startup times, improved UNIX filesystem support, new ASP.NET Core updates, and much more. To use the fastest version of .NET, you must upgrade to .NET 7.

In contrast, .NET 6 will continue to receive support through November of 2024, as it is considered a “Long Term Support” release. While it will not receive any of the feature upgrades of .NET 7, it will get security and bug fixes for longer. So if stability and support are your primary concerns, .NET 7 may not be for you.

Either way, it’s worth giving .NET 7 a look. So here, we’ll test a few logging frameworks with the .NET 7 preview and show them in action.

What Works on .NET 7?

In short, our favorite logging strategy for .NET 5 and .NET 6 works on .NET 7. Using Microsoft.Extensions.Logging as the logging API, then selecting a 3rd party logging framework and sink on top of it continues to be our preferred logging practice. Microsoft.Extensions.Logging provides tons of flexibility for implementing logging frameworks for different parts of your project while including other great features like logging scopes. Plus, it’s considered the default logging solution for .NET Core.

Now, let’s go over some 3rd party logging frameworks I tested with the preview: Serilog and Loupe. Here, I set up the logging frameworks using the minimal hosting model (introduced in .NET 6) with the template ASP.NET Core app. Additionally, I included a warning log on the home page and an error log on the privacy page.

But first, here’s a screenshot of the session data from Loupe Desktop, showing some information on the machine I used to run the tests and the .NET 7 version tag:

Screenshot of session details tab in Loupe desktop, showing that the .NET version is 7.0.0, the OS is windows 10, and some other app data

Serilog

The process of setting up Serilog for .NET 7 is identical to that for .NET 6. Create the logger config, select the sinks you want to use, then add Serilog as a logging provider. This is what it looks like in the minimal hosting model:

global using Loupe.Agent.Core.Services;
global using Loupe.Extensions.Logging;
global using Loupe.Serilog;
global using Serilog;

var builder = WebApplication.CreateBuilder(args);

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Debug()
    .WriteTo.File("logs/myapp.txt", rollingInterval: RollingInterval.Day)
    .WriteTo.Loupe()
    .CreateLogger();


// Add services to the container.
builder.Services.AddRazorPages();
builder.Logging.ClearProviders();
builder.Logging.AddSerilog();
builder.Host.AddLoupe();


In this case, I logged to both a text file and Loupe Desktop. Here are the resulting logs after running a quick session:

Text File

Text file showing Serilog warning log in context

Loupe Desktop

Loupe Desktop screenshot showing warning and error log in context

Overall, the Serilog setup is pretty simple. You don’t need a separate configuration file and can write to any sink with a dedicated package. If you wanted to log to file on .NET 7, or log to multiple sinks simultaneously, Serilog is a good option.

Loupe

Setting up Loupe to log directly with Microsoft.Extensions.Logging is straightforward. Considering all the logs are appended and formatted on the sink side, we get to skip a lot of in-code templating you need to do with the other frameworks. I can simply add Loupe into the startup, add a few lines to the appsettings.json, and I can access a production-grade log sink:

Screenshot of Loupe, showing the warning and error logs, as well as the main method info logs from the startup

If you want to log to Loupe exclusively, we recommend using our Microsoft.Extensions.Logging package with no additional framework, as it requires little in-app setup. But as seen with the previous example, you can log to Loupe and other log sinks in parallel using Logging Frameworks like Serilog (or NLog). Everyone has their own preferences for logging, and Loupe can be adjusted to work to your tastes.

What About Centralized Logging on .NET 7?

Ultimately, centralized logging is more of a function of selecting the right log sink than any framework. Luckily, Loupe Server is easy to add to either configuration, as I only need to add a couple of lines to the appsettings.json:

  //...  
    "Server": {
      "UseGibraltarService": true,
      "CustomerName": "Your Loupe Service Name",
      "AutoSendSessions": true,
      "SendAllApplications": true
    },
//...


And I can log .NET 7 application logs to Loupe 5, where it’s easy to jump between sessions and view all my log data:

Screenshot of Loupe 5, showing .NET 7 application logs

If you want to see centralized logging on .NET 7 for yourself, we have a free trial! It provides centralized logging for .NET applications with very few changes required in-app. You can learn more about Loupe 5 through the trial in the link below.

See The Trial

Rock solid centralized logging

Unlimited applications, unlimited errors, scalable from solo startup to enterprise.