Centralize your logs with Better Stack
While Serilog handles structured logging in your application, Better Stack provides centralized log management with live tailing, SQL and PromQL queries, automated anomaly detection, and incident management.
Serilog is an easy-to-set-up logging library for .NET with a clear API. In the long list of the Serilog's features you can find:
@ operatorSerilog is useful in the simplest small applications as well as in large and complex ones. Due to its rich configuration abilities, you can use it in all your projects.
In this tutorial you will learn how to:
You will need:
While Serilog handles structured logging in your application, Better Stack provides centralized log management with live tailing, SQL and PromQL queries, automated anomaly detection, and incident management.
To get started, you need to create a new project. You can do it in several ways in Visual Studio. The first one is to select Create a new project in Visual Studio start window.
If the Visual Studio IDE is already open, you can follow the path on the top menu bar *File > New > Project *and create a new project this way.
The last way is by using the shortcut: Ctrl + Shift + N.
Before starting to work on the application, you need to install some dependency packages. Visual Studio provides multiple ways to use the NuGet Package Manager. In the tutorial we will use the Package Manager Console.
The first thing you have to do is to open the Package Manager Console. You can
do it using Tools > NuGet Package Manager > Package Manager Console.
Alternatively, you can sequentially press ALT, V, E, and O.
In the console that opens, run the following commands one by one.
The Serilog package contains the types. While the Serilog.Sinks namespace
includes logging providers, such as Console, Debug, File, HTTP, and others.
Now, you are ready to create a logger with the basic configuration.
For the application, we're going to use the Serilog's levels system. The system consists of the 6 levels:
Default level, if no minimum level is specified, is the information level.
Creating a logger in Serilog is pretty straightforward. You can get the working logger in a few lines.
The following code should be written in the Program.cs file:
Serilog has rich configuration abilities and provides 2 ways how to configure the logger: programmatically or via a configuration file. You can get advanced information about each of them in the documentation.
In the tutorial we're going to configure the logger programmatically. We've decided to add 2 extra logging targets for the application.
The first logging target is a file for warnings and higher severity logs structured in JSON. Serilog has a built-in JSON formatter which we are going to use in the logger.
The second logging target is a rolling file for all logs. A rolling file is a file used for a certain amount of time. After the rolling interval will pass, the program will automatically create a new file with a date stamp of the creation in the name.
We're going to use 2 extra classes. To create a new file for a class, you can
press CRTL + SHIFT + A and select C# class in the window that opens.
The first file will contain a Person class. You can see its code in the
snippet below.
The second file will contain a Car class. You can see its code in the snippet
below.
Also, if you'd like to reduce the byte count of the log events, you may prefer
the CompactJsonFormatter instead of the Serilog's default JsonFormatter.
To demonstrate how the logger works, we will log some simple messages.
Now, let's build and run the program. You can simply do this by pressing
CTRL + F5.
After the execution, your console's output should look like:
Now, let's check the logs written in the files. The files are located in the
same folder as your executable. In our case it is
C:\\Users\\username\\source\\repos\\SerilogAdvanced\\SerilogAdvanced\\bin\\Debug\\netcoreapp3.1.
First, your important.json file should look like.
Second, the file for all logs will contain the current date. In our case, this
is all-20210507.logs.
Once you've configured Serilog in your .NET application, the next step is centralizing those logs for effective monitoring and analysis across all your services. Better Stack provides OpenTelemetry-native log management.
Better Stack works seamlessly with Serilog's structured JSON output. You can continue logging to files and console with Serilog while using Better Stack's collector or log shippers like Vector to forward logs to centralized storage. This keeps your application code clean while enabling powerful querying with SQL, PromQL, or live tail filtering—all executing in sub-seconds even across terabytes of data.
Live tail your .NET logs in real-time
Monitor your Serilog output as it happens with Better Stack's live tail feature. Filter by log level, search for specific properties, or track errors across your .NET services instantly—perfect for debugging during development or monitoring production deployments.
Visualize your application health
Better Stack automatically parses your Serilog properties and lets you create custom dashboards to visualize error rates, response times, and application metrics. Track trends over time, set up alerts on anomalies, and gain insights into your .NET application's behavior without writing complex queries.
Getting started takes just minutes: sign up for a free account, configure Serilog to output JSON, and forward your logs using Better Stack's collector or your preferred log shipper.
Proper logging can greatly assist in the support and development of your application. This may seem like a daunting task, but Serilog is a fast and configurable logging framework that greatly simplifies the task.
In the tutorial, you have configured your logging system with multiple logging targets for a .NET console application with Serilog.
Now developing and maintaining your .NET applications will be much easier!
We use cookies to authenticate users, improve the product user experience, and for personalized ads. Learn more.