.NET CORE LOGGING TO MYSQL USING NLOG

.NET CORE LOGGING TO MYSQL USING NLOG

Set up the MySQL database

MySQL Workbench can be used to add the schema ‘nlog’ which will be used for logging to the MySQL database. The user ‘damienbod’ is also required, which must match the defined user in the connection string. If you configure the MySQL database differently, then you need to change the connection string in the nlog.config file.

You also need to create a log table. The following script can be used. If you decide to use NLog.Web in a ASP.NET Core application and add some extra properties, fields to the logs, then this script needs to be extended and also the database target in the nlog.config.

  • CREATE TABLE `log` (
  • `Id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  • `Application` varchar(50) DEFAULT NULL,
  • `Logged` datetime DEFAULT NULL,
  • `Level` varchar(50) DEFAULT NULL,
  • `Message` varchar(512) DEFAULT NULL,
  • `Logger` varchar(250) DEFAULT NULL,
  • `Callsite` varchar(512) DEFAULT NULL,
  • `Exception` varchar(512) DEFAULT NULL,
  • PRIMARY KEY (`Id`)
  • ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

The MySql.Data pre release NuGet package can be used to log to MySQL. Add this to your project.

About the Author

admin administrator

Leave a Reply