ASP.NET5 : Microsoft.Extensions.Logging to log in SQL Server

2.6k views Asked by At

I am working on ASP.NET5 Project where I am using Microsoft.Extensions.Logging as logging framework. Here I want to provide the different options for logging.The User can select logging into a text file or Database(SQL Server) itself.

I know how to do logging in the-the text file. But How I can configure Microsoft.Extensions.Logging to log data in SQL Server.

Any help will be really appreciated.

1

There are 1 answers

0
Chris Voon On BEST ANSWER

Take a look at Damienbod's blog at https://damienbod.com/2016/08/17/asp-net-core-logging-with-nlog-and-microsoft-sql-server/.

It comes with a sample project that writes to both text and database.

  1. Clone the source from https://github.com/damienbod/AspNetCoreNlog Set AspNetCoreNlog as startup project.
  2. Create a SQL database on any of your SQL server instance, say '(localdb)\mssqllocaldb'. Name the database, say 'AspNetCoreNlog'.
  3. Create the database table using the CREATE script provided in README.md.
  4. Update ConnectionStrings for NLogDb in appsettings.json to

    "Data Source=(localdb)\mssqllocaldb;Initial Catalog=AspNetCoreNlog;Integrated Security=True;"

  5. Run the project.
  6. Check logs created in "C:\git\damienbod\AspNetCoreNlog\Logs" folder. NLog log files
  7. Check logs created in SQL database Log table. NLog SQL logs

It will takes about 10 minutes to setup and run.