Configuring Serilog SQL server sink logging for ASP.Net Core application

Here are the changes that you need to make in your ASP.Net Core web application to configure Serilog logging with SQL server sink.

a. Install the following libraries from Nuget, using the Nuget Package Manager
Serilog.AspNetCore
Serilog.Extensions.Logging
Serilog.Settings.AppSettings
Serilog.Settings.Configuration
Serilog.Sinks.MSSqlServer

b. Program.cs:


using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Serilog;
using System;
using System.IO;

namespace Scrubber
{
  public class Program
  {
    public static void Main(string[] args)
    {
      var path = Directory.GetCurrentDirectory();
      var environmentName =Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production";

      var configuration = new ConfigurationBuilder()
      .SetBasePath(path)
      .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
      .AddJsonFile($"appsettings.{environmentName}.json", optional: false, reloadOnChange: true)
      .Build();

      Log.Logger = new LoggerConfiguration()
        .Enrich.FromLogContext()
        .ReadFrom.Configuration(configuration)
        .CreateLogger();

      try
      {
        WebHost.CreateDefaultBuilder(args)
          .UseStartup()
          .UseSerilog()
          .Build()
          .Run();
      }
      finally
      {
        Log.CloseAndFlush();
      }
    }
  }
}

AppSettings.json:


{
  "Serilog": {
    "Using": [ "Serilog.Sinks.MSSqlServer" ],
    "MinimumLevel": "Information",
    "WriteTo": [
      {
        "Name": "MSSqlServer",
        "Args": {
          "connectionString": "YourSQLServerConnectionString",
          "tableName": "Logs",
          "autoCreateSqlTable": true
        }
      }
    ]
  }
}

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.