ASP.NET Core custom logging provider to SQL Server database

This demo allows the use of the ILogger and ILoggerProvider interfaces to create a custom logging provider that logs to a SQL Server database.

It integrates with an ASP.NET Core Web API and it's appsettings.json file to log errors to a SQL Server database.

Inside the appsettings.json, there is a Database object which allows you to configure the severity of logging. There are also settings to change the connection string, which fields to include in the log, and the table name where the logs will be stored.

Software

This is the software that will need to be installed onto your machine.

  • Visual Studio 2022 (17.0.5 or above). It will work with the free community version. Please note, this project will not work with VS 2019.
  • .NET 6.0 SDK (6.0.100 or above). Visual Studio 2022 should already have the .NET SDK installed.
  • SQL Server 2017 (or above).

Get the application working

These are the steps to get the application working.

  • Fill out the source code form. We will send you an email where you can download the source code.

You are now ready to go.

Set up the database

Downloading the source code will guide you to a GitHub repository. In there, there is a Database folder. The Database folder containers two files. RoundTheCode_DbLogger.bak and RoundTheCode_DbLogger.bacpac. You will need to use one of these files to import the database to your SQL Server database.

Open the project in Visual Studio

Open up RoundTheCode.LoggerDb.sln in Visual Studio 2022 and you'll see that the custom logging provider code is in the RoundTheCode.LoggerDb.Shared project. In appsettings.json, make sure that the connection string is correct.

To run the ASP.NET Core Web API, you can hit https://localhost:7234. To get the exception, you can hit https://localhost:7234/weatherforecast.

More resources

Watch our video where we do a code walkthrough and demo of the custom logging provider that logs to a SQL Server database.

In addition, read about the code elements that create a custom logging provider with ASP.NET Core's ILogger.

Join our Discord server

  • Can't get your ASP.NET Core application to work? Ask for help here!
  • Discussions with other ASP.NET Core developers.
  • General chat with the community.
Join Discord