Boost your .Net 6.0 skills with Serilog!

Log with Serilog in .net 6.0

Quick Summary: Discover the power of the popular logging library “Serilog.” It allows you to capture and manage log data. Additionally, it enables you to identify and resolve issues very easily. And these are only some of Serilog’s features; there is much more to know. So, examine this article to learn how to log in with Serilog in your .NET 6.0 applications.


Logging is important for any .NET application. It helps you track errors, troubleshoot problems, and know how your application is doing. In addition, Serilog is a powerful logging library that makes it easy to log your application’s events and messages.

Additionally, Serilog is a popular, well-maintained logging library. Further, it supports various sinks to log your application’s events and messages to multiple destinations.

Moreover, the structure of Serilog has capabilities that make it easy to filter, search, and analyze your logs. If you are looking for a powerful and flexible logging library for your .NET 6.0 application, then Serilog will be the perfect choice.

Wondering why and how?

So, keep reading and get your answers. And Hire ASP.NET Core specialists to get easy development services.

What is Serilog?

We hope you know about .NET 6, but if not, then take a Deep dive into .NET 6.

Now let’s start with the overview of Serilog.

Logging is one of the most crucial things in Application development and It helps to troubleshoot any application issues.

Like many libraries in .Net, Serilog is a structured logging framework that provides diagnostic logging in files, consoles, databases, and elsewhere and it is easy to set up. Structured logging provides a message format for application Logs and treats them as data sets rather than plain text.

Serilog is so simple that you can send logs via simple configuration. Serilog uses sinks to send you logs to a text file, database, or log management solution.

What are Sinks in Serilog

Sinks are the targets where you want to send your logs and the most popular sinks are file and console targets.

If you want to create a project in .Net 6, First you have to download the latest SDK version and even you have to update your visual studio code or Visual studio.

As a result, Here is the link to download the latest SDK for .Net 6:

Why do you need to consider Serilog instead of other logging libraries?

1. Scaling performance

Serilog can help you to develop scalable, even for large-scale applications. It has a cleverly optimized architecture that can minimize performance overheads. Also, Serilog has a sink architecture that can direct log messages to different destinations, such as databases, cloud storage, and files. Hence this scalability will let Serilog overcome all logging needs of applications irrespective of size.

2. Extensibility

Serilog has extensibility which is one of the most favored advantages. The extensibility principle forms the basis of this framework, allowing customers to customize logging behavior to meet their requirements. Hence with the help of custom sinks to send logs to any endpoint or log events along with additional contextual information using enrichers. It is one of the versatile solutions as it has the flexibility to extend and tailor solutions.

3. Flexible and structured logging

Other libraries, rather than Serilog, produced plain text logs. But this framework can help you with structured logging capacities. It facilitates developers to log events and related properties in the proper format, like XML. Hence this structure will help in more accessible log analysis and processing. Also, when you deal with large amounts of logs, it gets easier. Developers can also use template messages to compose log events dynamically, along with relevant data.

4. Integration with popular .NET frameworks:

Serilog seems to integrate differently. NET-based networks. Hence it is effortless to use in other projects. Whether you want to build a web application using ASP.NET Core or a website application with .NET Core, Seeilog can be helpful. It is easy to use for smoothly implementing logging. Hence it will ensure that you can make the most out of Serilog without any problems, irrespective of the tech stack.

5. Community Support:

Serilog boasts a vast and robust community of developers that can provide continuous support and development. Hence the active community can result in frequent updates, fixing bugs, and enhancements. It also helps to ensure that you find many resources, tutorials, and plugins to make it easier for new users.

How to configure Serilog?

1. Install Serilog Package

Before you initiate configuring Serilog, all you have to do is install the package in the .NET application. Hence you can do this with the help of .NuGet Package Manager Console or Package Manager UI.

2. Choose a Sink

Later you will have to choose Sinks at the place where you want to store logs. It supports different sinks like Serilog. Sinks File for logging into a file, Serilog.Sinks.Console for logging to the console, Serilog.Sinks.Seq for logging into Seq and many others. You can install additional sinks using NuGet in the same way as you did for Serilog.

3. Configure Serilog in Code

Serilog should be configured in your application’s ‘Main’ method or during startup. ‘Program. Cs’ or a similar starting file is often where the configuration is done. Import the required namespaces first.

4. Log Messages

You may now begin logging messages across your application after setting up Serilog. Use the ‘Log’ class to log messages.

Different Serilog log levels, including Information, Debug, Warning, Error, and Fatal, are supported by Serilog. You can select the appropriate level based on the severity of the log message.

5. Customize the Configuration

The Serilog configuration can be further altered to meet the requirements of your application. With Serilog, you may add more properties to log events, filter logs based on specific criteria, and use several output formats.

To reduce log file sizes and preserve log history, you may, for instance, add enrichers, alter the log format, or define rolling file settings.

6. Explore Other Serilog Features

Serilog has a wealth of sophisticated capabilities, including context logging, structured logging, and custom sinks.

Logging Best Practices with Serilog

1. Choosing the Right Log Level for Different Scenarios

It is imperative to choose the proper log level to achieve the ideal balance between the volume of information logged and the relevance of the logged messages. Serilog has several log levels, including “Verbose,” “Debug,” “Information,” “Warning,” “Error,” and “Fatal.” When selecting log levels, take into account the following suggestions:

  • Verbose and Debug levels are frequently utilized during the development and debugging phases. They offer thorough details that are helpful for troubleshooting and comprehending the inner workings of the application.
  • Information: Record significant application events at this level to monitor how it functions normally.
  • Log events at this level to warn of potential errors that don’t currently impair the application’s functioning but may in the future.
  • Error and Fatal: Only critical errors and exceptions that require urgent attention should be recorded at these levels. They flag problems that could prevent the application from operating normally.

By selecting the appropriate log level, you can ensure that your log files contain the essential data without becoming overburdened with log messages.

2. Avoiding Common Logging Mistakes

Although logging is a valuable tool, it may be mishandled or cause issues if utilized improperly. Here are some typical logging errors to prevent:

  • Avoid overlogging: Especially at the ‘Verbose’ and ‘Debug’ levels, in production contexts. It may result from performance problems, higher storage costs, and trouble locating pertinent data.
  • Ignoring Exceptions: Always record errors and exceptions, even if they are recognized and dealt with. Leaving out these records can make it more difficult to identify problems later.
  • Logging Sensitive Data: Do not log private information or sensitive data like passwords or credit card details. It will be covered in greater detail in a subsequent section.
  • Lack of Context: Include enough context in your log messages, including pertinent identities, timestamps, and request/response information. Understanding the log entries during troubleshooting is made easier by this context.

3. Log Filtering and Performance Consideration

Log filtering becomes essential in production scenarios to minimize log volume and improve efficiency. Through its inherent characteristics and enrichments, Serilog provides filtering capabilities. Think about the following tactics:

  • Setting up Serilog to only log messages with a level equal to or higher than a predetermined threshold is known as minimum-level filtering. ‘Verbose’ and ‘Debug’ log levels, among others, can be filtered off in production.
  • Implement conditional logging following predetermined criteria, such as log messages about a particular module or feature. It keeps logs concentrated on important topics.
  • Enrichers: Serilog enrichers let you automatically add more contextual data to log events. As an illustration, consider adding user data, environment information, or request IDs.

4. Dealing with Sensitive Information in Logs

Data privacy and security must be considered when handling sensitive data in logs. Take into account the following procedures to prevent mistakenly logging sensitive data:

  • Masking: Use templates for log messages that automatically replace sensitive material with placeholders (such as “*********”).
  • Pseudonymization: If you must log certain information for debugging needs, pseudonymize it to prevent direct association with the actual user.
  • Secure Storage: Ensure log files containing sensitive information are safely kept, and only authorized individuals may access them.
  • Anonymization of logs: To abide by data protection laws, anonymizing logs may occasionally be required.

5. Using Serilog in Asynchronous Environments

Asynchronous processing is frequently used in modern systems to increase performance and scalability. The following should be kept in mind when utilizing Serilog in such settings:

  • Serilog is meant to be thread-safe, enabling you to simultaneously log from many threads.
  • Utilize Serilog’s async logging functionality to ensure that logging operations don’t obstruct the primary application thread.
  • Context Propagation: In asynchronous settings, ensure that contextual data (such as request IDs) propagates correctly between various threads and tasks, giving a clear view of the application’s execution flow.

Create a new .Net core WebApi application with the below command

dotnet new webapi -o SerilogWebApiApp

Then add the required package in your SerilogWebApiApp

dotnet add package serilog.aspnetcore
                 dotnet add package Serilog.Settings.Configuration
                 dotnet add package Serilog.Sinks.MSSqlServer

Initialize Serilog in appsettings.json file

"Serilog": {
                     "MinimumLevel": {
                     "Default": "Information",
                     "Override": {
                         "Microsoft": "Warning",
                         "Microsoft.Hosting.Lifetime": "Information"
                     "WriteTo": [
                         "Name": "MSSqlServer",
                         "Args": {
                         "tableName": "Logs",
                         "autoCreateSqlTable": true

Serilog defines different levels of log events. They are Verbose, Debug, Information, Warning, Error, and Fatal from low to high. Basically minimum level determines at which level log events are generated.

You can set the minimum level you want to log, which means an event for that level and by default, the server uses Error as minimumLevel of recording information.

"MinimumLevel": {
                     "Default": "Error",
  • You can change the setting of Default from Error to anything you need.
  • If I want to write log data to MsSqlServer, I wrote MSSqlServer in the Name field of the Serilog setting in the appsettings.json file.
  • I used connectionString in Args where I want to create a log table.
  • I want to create a table automatically, so I set the autoCreateSqlTable property as true.

As you know the program.cs and startup.cs are merged as program.cs in .Net 6 and as we are using.Net 6 here we have to configure Serilog in the program.cs, but if you are using .Net 5 you have to configure Serilog in a startup.cs.

Configure Serilog in Program.cs

Log.Logger = new LoggerConfiguration().CreateBootstrapLogger();
                 builder.Host.UseSerilog(((ctx, lc) => lc

You also have to add this line in configure of Program.cs


CreateBootstrapLogger() sets up Serilog so that the initial logger configuration (which writes only to Console), can be swapped out later in the initialization process, once the web hosting infrastructure is available.

Run SerilogWebApiApp project

dotnet watch

Call WeatherForecast API from Swagger and see the “Logs” table in SSMS.

Display log data

  • If any event occurs log will be added to the “Logs” table. We set it to default as information that’s why it adds a log of every event.
  • log
  • Serilog dotnet 6
  • Id: The id column is an optional table identity column. Generally, it defaults to int data type but is also configured as bigint. It is an auto-incrementing unique identity column.
  • Message: It stores the formatted output of log events and defaults to nvarchar(max).
  • MessageTemplate: This column stores log event messages with property placeholders.
  • Level: This column stores log events like information, error, etc.
  • TimeStamp: This column stores log event time with date.
  • Exception: When an exception occurs as a log event, the Exception message automatically appears here.
  • Properties: This column stored log event property value as XML.

Handling Errors and Exceptions with Serilog.

1. Logging Unhandled Exceptions

If not adequately managed, unhandled exceptions may result in unexpected behavior and potentially cause an application to crash. Many times you might find problems like Serilog not writing code. Serilog can catch and log these unhandled exceptions, providing helpful information about the underlying causes of problems.
Unhandled Exception Logging with Serilog

  • **Global Exception Handling**: You can listen to the ‘AppDomain.CurrentDomain.UnhandledException’ event to catch unhandled errors worldwide. You can use Serilog to log the exception inside the event handler.
  • ‘UseSerilogRequestLogging’ middleware you can use it in Serilog ASP.NET Core 6 applications to log unhandled exceptions and information about HTTP requests.

2. Implementing Custom Error Logging Strategies

With the help of Serilog, programmers can design unique error-logging techniques that can be useful for the error-handling procedure for specific use cases or needs. This flexibility makes better integration with current logging systems or outside services possible.

Making Error Logging Output Specific

You can modify the output of error logs by creating your own Serilog “LoggerConfiguration” and configuring the target sinks and formatting choices.

Sending Errors to External Services

Serilog integrates with various third-party logging services such as Elasticsearch, Seq, or Application Insights, making it easy to forward error logs.

Logging Errors in Distributed Systems

Due to the intricacy of interconnected components, error logging is more difficult in distributed systems. Serilog can help with efficient error management across several services.

Contextual logging and correlation

You must create correlation IDs to monitor the flow of requests between services. Serilog enrichers can enhance logs with contextual data, making tracking issues in various system components easier. Serilog enrichers can enhance logs with contextual data, making tracking issues in various system components easier.

Logging in one place

Centralizing all service logs to a single logging infrastructure in dispersed settings is advantageous. Serilog sinks like Elasticsearch and Logstash and cloud-based services like Azure Monitor or AWS CloudWatch.

Testing and Debugging Serilog in .NET 6.0

1. Testing Logging Behavior with Serilog

Practical testing of the logging behavior is necessary to ensure the precision and thoroughness of log entries. Here are some tactics to take into account:

  • Use Xunit Unit Testing in ASP.NET Core to confirm that the anticipated log messages are generated when evaluating logging behavior at a detailed level. You may assert against the log entries by mocking the Serilog logger and collecting the log output, ensuring the logging code works correctly.
  • Interaction testing is helpful when the logging behavior depends on the seamless interaction of several components. These tests look into how Serilog interacts with other application components and confirm that the logging functions as intended in practical situations.
  • Serilog.Sinks.In multi-threaded systems, you can test the correlation of log entries using the specialized package TestCorrelator. This package correctly links specific activities or request logs, simplifying debugging in complex systems.

2. Debugging Common Serilog Issues

Despite its dependability, employing Serilog may cause several typical problems for developers. Here are a few specific issues and approaches to solving them:

  • Missing Log Entries: If you notice any missing log entries, make sure to check the log levels and configured sinks. Ensure that the log level captures all necessary log messages and that the sinks, such as the database, file, or console, have proper permissions and are operational.
  • Formatting Problems: Improper log message formatting might be confusing when reading logs. Verify again that the placeholders in your log message templates match the provided log data. Effective debugging and log analysis depend on properly prepared records.
  • Performance issues: Logging overhead may affect performance in high-throughput applications. Pay attention to the performance metrics to improve logging performance and consider employing Serilog’s batching or buffering options.

3. Best Practices for Troubleshooting Serilog Configurations

Observe these best methods to debug Serilog configurations efficiently:

  • Enable self-logging to collect internal diagnostic data from the library itself. Serilog enables self-logging. This function can troubleshoot problems with the Serilog configuration and sinks.
  • Contextual logging: Use Serilog’s structured logging features to add pertinent contextual data to log messages. This method significantly aids in understanding the flow of the application and identifying problems in systems.
  • Using Serilog Diagnostic Context: Use Serilog’s diagnostic context to add dynamic, context-specific attributes to log entries. By connecting logs to particular activities or requests, this functionality makes it simpler to track down problems across various components.


In conclusion, using Serilog with.NET 6.0 offers developers an effective and dependable logging solution. Serilog accelerates the process of logging application events and offers useful insights for debugging, performance optimization, and auditing because of its simple configuration and robust features. Structured and contextual logging information improves the simplicity of monitoring and troubleshooting in complicated systems.

Additionally, developers can customize logging output to meet their unique needs thanks to the smooth connection with various sinks, ensuring flexibility and scalability. Using Serilog enables.NET 6.0 developers to create robust, maintainable applications, enhancing the development process and raising the caliber of the product as a whole.


Serilog is a widely used logging library in the .NET ecosystem, offering a flexible and efficient solution for logging messages and events in .NET 6.0 applications. With Serilog’s structured logging capabilities, analyzing and filtering logs becomes effortless, thereby enhancing troubleshooting and application monitoring.

To integrate Serilog into your .NET 6.0 project, install the ‘Serilog’ and ‘Serilog.AspNetCore’ NuGet packages. Once installed, configure Serilog within the ConfigureLogging method of your Startup class by adding a new logger configuration through the UseSerilog extension method.

Absolutely! Serilog offers the flexibility to log messages to different sinks, such as files, the console, databases, or even third-party services like Seq or Elasticsearch. It allows you to set up multiple sinks simultaneously, storing and analyzing log data based on your specific requirements.

Structured logging is a prominent feature of Serilog, providing logs with well-organized and searchable data. To enable structured logging, utilize Serilog’s ‘LoggerConfiguration’ to define properties in a structured manner. This approach makes it easier to filter and analyze logs effectively, enhancing the overall logging experience.

Yes, Serilog fully supports various logging levels, which assist in categorizing log messages according to their severity. In ascending order of severity, the logging levels include Verbose, Debug, Information, Warning, Error, and Fatal. By leveraging these levels, developers can log messages with appropriate importance and easily control the verbosity of logs as needed for better debugging and monitoring.