In addition, read about the code elements that create a custom logging provider with ASP.NET Core's ILogger.
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.
This is the software that will need to be installed onto your machine.
These are the steps to get the application working.
You are now ready to go.
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 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
.
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.