SoFunction
Updated on 2025-03-01

C# Methods of using Serilog logging framework

Serilog is a convenient configuration and flexible logging framework. The usage method is as follows:
1. Log output to the console, you need to use Nuget to install Serilog and two packages

// Initialize the shared instance of the log             = new LoggerConfiguration()
                .()
                .()
                .()
                .CreateLogger();
            // Write to log            ("Info");

2. Log output to file, package is required

// Initialize the shared instance of the log             = new LoggerConfiguration()
                .()
                .()
                .("logs/", 
                    rollingInterval: , // One log file per day                    shared: true    // Allow other processes to share log files                ).CreateLogger();
            // Write to log            ("Info");

3. You can configure different log levels to output to different log files

// Initialize the shared instance of the log             = new LoggerConfiguration()
                .()
                .()
                // Debug log                .(x => x
                    .(e =>  == )
                    .("logs/", rollingInterval: , shared: true)
                // Error log                ).(x => x
                    .(e =>  == )
                    .("logs/", rollingInterval: , shared: true)
                // Application log                ).(x => x
                    .(e =>  !=  &&
                         != )
                    .("logs/", rollingInterval: , shared: true)
                ).CreateLogger();
            // Write to the log            ("Info");
            // Write to the log            ("Debug");
            // Write to the log            ("Error");

4. Write logs to SqlServer

// Configuration to write to the database            var sinkOpts = new MSSqlServerSinkOptions();
             = "logs";            // Log table name             = true;     // Automatically create tables            // Column configuration            var columnOpts = new ColumnOptions();
            ();  // Remove the exception column            (); // Remove attribute column             = 2048;             // Specify the length of the column             = true;      // Remove the clustered index of timestamps            // Initialize the shared instance of the log             = new LoggerConfiguration()
                .()
                .()
                .(
                        connectionString: ["Default"].ConnectionString,
                        sinkOptions: sinkOpts,
                        columnOptions: columnOpts
                 ).CreateLogger();
            // Write to log            ("Info");

After executing the code, we found that the log table was not added to the database and there was no exception information. We can add the following code to debug Serilog

(msg =>
            {
                (msg);
                ();
            });

Get the following error message:

2022-11-26T09:34:53.6107406Z Unable to write 1 log events to the database due to following error: A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - The certificate chain is issued by an untrusted authority.)

The configuration that needs to add a trust server certificate to the connection string: TrustServerCertificate=True;

You can create a log table in SqlServer and write it to the log.

This is the end of this article about C# using Serilog log framework. For more related content of C# Serilog log framework, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!