DEV Community

Cover image for Advanced Logging in ASP.NET Core with Serilog
Tapesh Mehta for WireFuture

Posted on

Advanced Logging in ASP.NET Core with Serilog

Introduction

In today's blog post we'll learn how to do advanced logging in ASP.NET Core with Serilog. Logging is a vital part of any business application and offers visibility into application behaviour, performance and possible issues. Among the logging frameworks available for .NET, Serilog offers a flexible, structured logging and multiple sink integration. This article will explain why you should use Serilog, how to configure it for different sinks and enrichers, and best practices for structured logging in enterprise applications.

For those interested in learning more about .NET development, check out our .NET Development blogs. Stay updated with the latest insights and best practices!

Why Use Serilog for Logging?

  1. Structured Logging: Serilog's primary strength is its support for structured logging, which allows you to capture detailed, queryable log data.

  2. Flexibility: Serilog is highly configurable and supports numerous sinks (outputs) and enrichers (additional data in logs).

  3. Ease of Use: With a simple and fluent configuration API, setting up Serilog in your application is straightforward.

  4. Performance: Serilog is designed for high-performance logging, ensuring minimal overhead on your application.

Configuring Serilog for Various Sinks and Enrichers

Let's dive into configuring Serilog in an ASP.NET Core application, focusing on different sinks and enrichers.

Step 1: Setting Up Serilog

First, add the necessary Serilog packages to your project. You can do this via NuGet Package Manager or by running the following commands in the Package Manager Console:

Install-Package Serilog.AspNetCore
Install-Package Serilog.Sinks.Console
Install-Package Serilog.Sinks.File
Install-Package Serilog.Sinks.MSSqlServer
Install-Package Serilog.Sinks.Datadog
Install-Package Serilog.Enrichers.Environment
Install-Package Serilog.Enrichers.Thread
Enter fullscreen mode Exit fullscreen mode

Step 2: Configuring Serilog in Program.cs

In the Program.cs file, configure Serilog as the logging provider for your ASP.NET Core application:

using Serilog;

public class Program
{
    public static void Main(string[] args)
    {
        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .Enrich.WithEnvironmentName()
            .Enrich.WithThreadId()
            .WriteTo.Console()
            .WriteTo.File("logs/log.txt", rollingInterval: RollingInterval.Day)
            .WriteTo.MSSqlServer(
                connectionString: "YourConnectionString",
                sinkOptions: new Serilog.Sinks.MSSqlServer.MSSqlServerSinkOptions { TableName = "Logs" })
            .WriteTo.DatadogLogs(
                apiKey: "YourDatadogApiKey",
                source: "YourApplicationName")
            .CreateLogger();

        try
        {
            Log.Information("Starting up");
            CreateHostBuilder(args).Build().Run();
        }
        catch (Exception ex)
        {
            Log.Fatal(ex, "Application start-up failed");
            throw;
        }
        finally
        {
            Log.CloseAndFlush();
        }
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .UseSerilog()
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Enriching Logs

Enrichers add valuable context to your log entries. Here, we've used environment and thread ID enrichers. You can add more enrichers as needed:

.Enrich.WithEnvironmentName()
.Enrich.WithThreadId()
Enter fullscreen mode Exit fullscreen mode

Step 4: Detailed Configuration for Various Sinks

Console Sink

The console sink outputs logs to the console, which is useful during development:

.WriteTo.Console()
Enter fullscreen mode Exit fullscreen mode

File Sink

The file sink writes logs to a file, with options for rolling logs daily:

.WriteTo.File("logs/log.txt", rollingInterval: RollingInterval.Day)
Enter fullscreen mode Exit fullscreen mode

SQL Server Sink

The SQL Server sink stores logs in a database, allowing for robust querying and analysis:

.WriteTo.MSSqlServer(
    connectionString: "YourConnectionString",
    sinkOptions: new Serilog.Sinks.MSSqlServer.MSSqlServerSinkOptions { TableName = "Logs" })
Enter fullscreen mode Exit fullscreen mode

Ensure you have a table in your database to store the logs. You can create it using the following SQL script:

CREATE TABLE Logs (
    Id INT IDENTITY(1,1) PRIMARY KEY,
    Message NVARCHAR(MAX),
    MessageTemplate NVARCHAR(MAX),
    Level NVARCHAR(128),
    TimeStamp DATETIMEOFFSET,
    Exception NVARCHAR(MAX),
    Properties NVARCHAR(MAX)
);
Enter fullscreen mode Exit fullscreen mode

Datadog Sink

The Datadog sink sends logs to Datadog, a popular monitoring and analytics platform:

.WriteTo.DatadogLogs(
    apiKey: "YourDatadogApiKey",
    source: "YourApplicationName")
Enter fullscreen mode Exit fullscreen mode

Replace "YourDatadogApiKey" with your actual Datadog API key and set an appropriate source name for your application.

Best Practices for Structured Logging in Enterprise Applications

  1. Use Structured Logging: Capture logs in a structured format to enable advanced querying and analysis.

  2. Log at Appropriate Levels: Use different log levels (e.g., Debug, Information, Warning, Error, Fatal) to capture the right amount of detail.

  3. Avoid Sensitive Information: Ensure that sensitive data (e.g., passwords, personal information) is not logged.

  4. Use Enrichers: Add contextual information to your logs using enrichers to make them more informative.

  5. Centralize Logging: Store logs in a central location (e.g., SQL Server, Datadog) to facilitate monitoring and troubleshooting.

  6. Monitor Log Size and Performance: Regularly monitor the size and performance impact of your logs, and configure log rotation or retention policies as needed.

Comparison of Serilog, NLog, and log4net for ASP.NET Core Logging

NLog and log4net are other popular logging libraries used in the .NET ecosystem. Both have a strong user base and offer extensive features for various logging needs. However, Serilog stands out with its advanced structured logging capabilities, flexible configuration, and a wide range of built-in enrichers and sinks. The table below provides a detailed comparison of these three logging frameworks, highlighting their strengths and differences to help you choose the best option for your ASP.NET Core application.

Feature Serilog NLog log4net
Structured Logging Yes Limited Limited
Performance High performance High performance Moderate performance
Enrichers Extensive support for enrichers Basic support Basic support
Sinks/Appenders Extensive (Console, File, SQL, Datadog, etc.) Extensive (Console, File, Database, etc.) Extensive (Console, File, Database, etc.)
Asynchronous Logging Yes Yes Yes
Community and Support Strong community and active development Strong community and active development Strong community but less active development
Documentation Excellent documentation and examples Good documentation Good documentation
Flexibility Highly flexible and easily extendable Highly flexible and easily extendable Highly flexible and easily extendable
Built-in Enrichers Yes (e.g., Environment, Thread, Machine Name) No built-in enrichers, custom development needed No built-in enrichers, custom development needed
Log Event Properties Structured properties with rich data types Structured properties, but less emphasis on richness Structured properties, but less emphasis on richness
Library Size Lightweight Lightweight Lightweight
Configuration Format Code-based, JSON, XML XML, JSON, code-based XML, code-based
Support for .NET Core Excellent support for .NET Core Excellent support for .NET Core Excellent support for .NET Core
Custom Sinks/Appenders Easy to create custom sinks Easy to create custom targets Easy to create custom appenders

Conclusion

Serilog is a powerful logging framework supporting structured logging in ASP.NET Core applications. By configuring Serilog with different sinks and enrichers, you can analyze your application's behavior and performance. Best practices for structured logging help you make your logs informative, manageable, and useful for diagnosing issues in your enterprise applications.

By following the steps outlined in this article, you can set up a robust logging system that enhances your application's observability and helps you maintain high reliability and performance.

Hope you find this blog post helpful. Happy coding and exploring with Serilog!

For those interested in learning more about .NET development, check out our .NET Development blogs. Stay updated with the latest insights and best practices!

Top comments (0)