Logging errors to a database using Serilog
Serilog is a third-party logging package for .NET applications. It focuses on fully structured events. It's known for simple APIs, easy setup, and lots of packages to log to various sources, such as files, databases, ElasticSearch, Raygun, and so on.
We will write log information to a database (an MS SQL Server database, that is) using Serilog.Sinks.MSSqlServer
. We will add the following packages using the NuGet package manager:
Serilog: 2.5.1-dev-00873Serilog.Sinks.MSSqlServerCore: 1.1.0
For database logging, we need a database server location, name, and table. Open appsettings.json
to add these configuration settings:
"Serilog": { "ConnectionString":"Server=.\\sqlexpress; Database=MyWalletLogDB;trusted_connection=true", "TableName": "Logs" }
Run the SQL script found at this URL on MyWalletLogDB
to create a logs table. Refer to https://github.com/serilog/serilog-sinks-mssqlserver for more information.
In the ConfigureServices...