{"id":58840,"date":"2021-09-06T07:00:25","date_gmt":"2021-09-06T05:00:25","guid":{"rendered":"https:\/\/code-maze.com\/?p=58840"},"modified":"2021-12-22T11:55:12","modified_gmt":"2021-12-22T10:55:12","slug":"writing-logs-to-sql-server-using-nlog","status":"publish","type":"post","link":"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/","title":{"rendered":"Writing Logs to SQL Server Using NLog"},"content":{"rendered":"<p>We have <a href=\"https:\/\/code-maze.com\/net-core-web-development-part3\/\" target=\"_blank\" rel=\"noopener\">previously<\/a> learned how to log messages to a File using NLog. In this article, we are going to learn how to use Nlog to log messages into SQL Server. <br \/>\nWe are going to see how we can configure an ASP.NET Core Web API project to support NLog and how we can configure NLog using an XML configuration file.<\/p>\n<div style=\"padding: 20px; border-left: 5px #dc2323 solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">To download the source code for this article, you can visit our <a href=\"https:\/\/github.com\/CodeMazeBlog\/writing-logs-nlog-sqlserver\" target=\"_blank\" rel=\"nofollow noopener\">Writing Logs to SQL Server Using NLog<\/a> repository.<\/div>\n<p>So, let&#8217;s start<\/p>\n<h2 id=\"creatingDatabase\">Creating the Database and Table for Logging<\/h2>\n<p>Let\u2019s start by creating the database and a table that will store our log messages. Using SQL Server Management Studio, let\u2019s create our database and name it <code>Nlog<\/code>. To create the table and columns, right-click on the <code>Nlog<\/code> database, select <code>New Query<\/code> and execute the SQL script:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">CREATE TABLE Logs(\r\n    Id int NOT NULL PRIMARY KEY IDENTITY(1,1),\r\n    CreatedOn datetime NOT NULL,\r\n    Level nvarchar(10),\r\n    Message nvarchar(max),\r\n    StackTrace nvarchar(max),\r\n    Exception nvarchar(max),\r\n    Logger nvarchar(255),\r\n    Url nvarchar(255)\r\n);\r\n\r\n<\/pre>\n<p><span style=\"font-weight: 400;\">We are creating a <\/span><code><span style=\"font-weight: 400;\">Logs<\/span><\/code><span style=\"font-weight: 400;\"> table with 8 columns.<\/span> <span style=\"font-weight: 400;\">We are giving each column a name that describes the kind of log message it will store. After executing the script, we are going to have a new table with all the columns in the database:<\/span><\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/07\/empty-table-colums.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-58843 size-full\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/07\/empty-table-colums.png\" alt=\"Empty table columns in SQL Server\" width=\"844\" height=\"57\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/07\/empty-table-colums.png 844w, https:\/\/code-maze.com\/wp-content\/uploads\/2021\/07\/empty-table-colums-300x20.png 300w, https:\/\/code-maze.com\/wp-content\/uploads\/2021\/07\/empty-table-colums-768x52.png 768w\" sizes=\"auto, (max-width: 844px) 100vw, 844px\" \/><\/a><\/p>\n<h2 id=\"creatingProject\">Creating the Project<\/h2>\n<p>Now let\u2019s create a new ASP.NET Web API project.<\/p>\n<p>Open VS 2019 and select <code>Create a new project<\/code>, choose <code>ASP.NET Web API<\/code> and select <code>Next<\/code>.<\/p>\n<p>Let\u2019s name our project <code>Nlog.API<\/code> and the solution as <code>Nlog<\/code>:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/07\/new-pro.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-58846 size-full\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/07\/new-pro.png\" alt=\"New ASP.NET Core Web API project to log messages to SQL Server\" width=\"623\" height=\"361\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/07\/new-pro.png 623w, https:\/\/code-maze.com\/wp-content\/uploads\/2021\/07\/new-pro-300x174.png 300w\" sizes=\"auto, (max-width: 623px) 100vw, 623px\" \/><\/a>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<\/p>\n<p>Finally, let\u2019s choose <code>.NET 5.0<\/code> as our target framework and select <code>Create<\/code>:<\/p>\n<h2><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/07\/add-info.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-58847 size-full\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/07\/add-info.png\" alt=\"ASP.NET 5 Project additional information\" width=\"615\" height=\"419\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/07\/add-info.png 615w, https:\/\/code-maze.com\/wp-content\/uploads\/2021\/07\/add-info-300x204.png 300w\" sizes=\"auto, (max-width: 615px) 100vw, 615px\" \/><\/a>\u00a0<\/h2>\n<h2 id=\"installingPackages\">Installing Packages and Configuring NLog<\/h2>\n<p>Using the Nuget Package Manager on Visual Studio, we are going to install these packages:<\/p>\n<ul>\n<li>NLog 4.7.10<\/li>\n<li>NLog.Web.AspNetCore 4.13.0<\/li>\n<li>System.Data.SqlClient 4.8.2<\/li>\n<\/ul>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/07\/package-list.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-58848 size-full\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/07\/package-list.png\" alt=\"NLog packages for SQL Server logging\" width=\"556\" height=\"211\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/07\/package-list.png 556w, https:\/\/code-maze.com\/wp-content\/uploads\/2021\/07\/package-list-300x114.png 300w\" sizes=\"auto, (max-width: 556px) 100vw, 556px\" \/><\/a><\/p>\n<blockquote>\n<p>Note:<br \/>\nNot installing the System.Data.SqlClient package will result in this error:<\/p>\n<p><em>Failed to create ConnectionType from DBProvider=sqlserver<\/em><br \/>\n<em>Exception: System.IO.FileNotFoundException<\/em><\/p>\n<\/blockquote>\n<h3 id=\"updatingProgramCS\">Updating Program.cs<\/h3>\n<p>Now, let\u2019s update our <code>Program.cs<\/code> file to set up NLog:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"7-22,31-36\">namespace Nlog.API\r\n{\r\n    public class Program\r\n    {\r\n        public static void Main(string[] args)\r\n        {\r\n            var logger = NLogBuilder.ConfigureNLog(\"nlog.config\").GetCurrentClassLogger();\r\n\r\n            try\r\n            {\r\n                CreateHostBuilder(args).Build().Run();\r\n\r\n            }\r\n            catch (Exception exception)\r\n            {\r\n                logger.Error(exception, \"Stopped program because of exception\");\r\n                throw;\r\n            }\r\n            finally\r\n            {\r\n                NLog.LogManager.Shutdown();\r\n            }\r\n        }\r\n\r\n        public static IHostBuilder CreateHostBuilder(string[] args) =&gt;\r\n            Host.CreateDefaultBuilder(args)\r\n                .ConfigureWebHostDefaults(webBuilder =&gt;\r\n                {\r\n                    webBuilder.UseStartup&lt;Startup&gt;();\r\n                })\r\n                .ConfigureLogging(logging =&gt;\r\n                {\r\n                    logging.ClearProviders();\r\n                    logging.SetMinimumLevel(LogLevel.Trace);\r\n                })\r\n                .UseNLog();\r\n    }\r\n\r\n\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">To start logging, we need to create a <\/span><code><span style=\"font-weight: 400;\">Logger<\/span><\/code><span style=\"font-weight: 400;\"> instance. Before creating the Logger instance, we are configuring Nlog by passing the configuration file <\/span><code><span style=\"font-weight: 400;\">nlog.config<\/span><\/code><span style=\"font-weight: 400;\"> which we are going to create in the next section. The <\/span><code><span style=\"font-weight: 400;\">GetCurrentClassLogger()<\/span><\/code> method<span style=\"font-weight: 400;\"> returns the Logger instance with the <\/span><span style=\"font-weight: 400;\">name <\/span><span style=\"font-weight: 400;\">of the current class (Nlog.API.Program).<\/span><\/p>\n<p>The <code><span style=\"font-weight: 400;\">logging.ClearProviders()<\/span><\/code> method<span style=\"font-weight: 400;\"> clears the default logging providers added by <\/span><code><span style=\"font-weight: 400;\">Host.CreateDefaultBuilder<\/span><\/code> <span style=\"font-weight: 400;\">and we are calling <\/span><code><span style=\"font-weight: 400;\">UseNLog()<\/span><\/code><span style=\"font-weight: 400;\">, an extension method for <\/span><code><span style=\"font-weight: 400;\">IHostBuilder<\/span><\/code><span style=\"font-weight: 400;\">, to register NLog as a logging provider for our entire application and set it up for dependency injection.<\/span><\/p>\n<h3 id=\"updatingAppsettings\">Updating appsettings.json<\/h3>\n<p>By default, the minimum log level is set to <code>Information<\/code> in our appsettings.json file. This setting overrides the minimum log level we specify in Program.cs when we call <code>logging.SetMinimumLevel(LogLevel.Trace)<\/code>. Let\u2019s update the <code>Default<\/code> key in the <code>Logging<\/code> section of our appsettings.json file. Remember to update any environment-specific appsettings.json file as well:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"3\">\"Logging\": {\r\n    \"LogLevel\": {\r\n      \"Default\": \"Trace\",\r\n      \"Microsoft\": \"Warning\",\r\n      \"Microsoft.Hosting.Lifetime\": \"Information\"\r\n    }\r\n }<\/pre>\n<h3 id=\"creatingNlogConfig\">Creating NLog.config File<\/h3>\n<p>Now, let\u2019s create our <code>nlog.config<\/code> file.<\/p>\n<p>In the project root, let\u2019s create a text file and name it <code>nlog.config<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">&lt;?xml version=\"1.0\" encoding=\"utf-8\" ?&gt;\r\n&lt;nlog xmlns=\"http:\/\/www.nlog-project.org\/schemas\/NLog.xsd\"\r\n      xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n      autoReload=\"true\"\r\n      internalLogLevel=\"Trace\"\r\n      internalLogFile=\"C:\\Nlog\\logs\\internalLog.txt\"&gt;\t\t\t\r\n                \r\n    &lt;targets&gt;\r\n        &lt;target xsi:type=\"Database\"\r\n      name=\"dbTarget\"\r\n      connectionString=\"Data Source=localhost;Initial Catalog=Nlog;Integrated Security=true;\"\r\n      commandText=\"INSERT INTO Logs(CreatedOn,Message,Level,Exception,StackTrace,Logger,Url) VALUES (@datetime,@msg,@level,@exception,@trace,@logger,@url)\"&gt;\r\n            &lt;parameter name=\"@datetime\" layout=\"${date}\" \/&gt;\r\n            &lt;parameter name=\"@msg\" layout=\"${message}\" \/&gt;\r\n            &lt;parameter name=\"@level\" layout=\"${level}\" \/&gt;\r\n            &lt;parameter name=\"@exception\" layout=\"${exception}\" \/&gt;\r\n            &lt;parameter name=\"@trace\" layout=\"${stacktrace}\" \/&gt;\r\n            &lt;parameter name=\"@logger\" layout=\"${logger}\" \/&gt;\r\n            &lt;parameter name=\"@url\" layout=\"${aspnet-request-url}\" \/&gt;\r\n        &lt;\/target&gt;\r\n    &lt;\/targets&gt;\r\n\r\n    &lt;rules&gt;\r\n        &lt;logger name=\"*\" minlevel=\"Trace\" writeTo=\"dbTarget\" \/&gt;\r\n    &lt;\/rules&gt;\r\n&lt;\/nlog&gt;<\/pre>\n<p>Our configuration file contains a single target and a rule. We create a database target and insert the values that will be stored in our <code>Logs<\/code> table each time we log a message.<\/p>\n<p><span style=\"font-weight: 400;\">The <\/span><code><span style=\"font-weight: 400;\">commandText<\/span><\/code><span style=\"font-weight: 400;\"> attribute contains an SQL statement to insert the values. We are not passing the values directly, instead, we are passing parameter names(e.g. @msg) that hold the values. The value associated with each parameter name is what gets returned from the <\/span><code><span style=\"font-weight: 400;\">layout<\/span><\/code><span style=\"font-weight: 400;\"> attribute. Each layout attribute returns the value of the <\/span><code><span style=\"font-weight: 400;\">layout renderer<\/span><\/code><span style=\"font-weight: 400;\"> that we pass to it.<\/span><\/p>\n<p><code><span style=\"font-weight: 400;\">${<\/span><span style=\"font-weight: 400;\">date}<\/span><\/code><span style=\"font-weight: 400;\">\u00a0and <code>${logger}<\/code> are examples of layout renderers that return the current date and time and the logger name respectively.<\/span><\/p>\n<p>NLog provides us with several layout renderers. For a full list of all layout renderers, please check <a href=\"https:\/\/nlog-project.org\/config\/?tab=layout-renderers\" target=\"_blank\" rel=\"nofollow noopener\">here.<\/a><\/p>\n<blockquote>\n<p>Note:<br \/>\nA misspelled name of any of the layout renderers, for example, <code>${trace}<\/code> instead of <code>${stacktrace}<\/code> will result in this error:<\/p>\n<p><em>Error parsing layout<\/em><br \/>\n<em>Exception: System.ArgumentException: LayoutRenderer cannot be found<\/em><\/p>\n<\/blockquote>\n<p>Ensure to change the <code>connectionString<\/code> and <code>internalLogFile<\/code> path to match yours. The internal log file is useful for debugging NLog.<\/p>\n<h2 id=\"writingLogs\">Writing Logs in the Controller<\/h2>\n<p>Using the <code>WeatherForecastController<\/code> class that was generated for us in the <code>Controller<\/code> folder, let\u2019s edit it to log messages:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"17-25\">namespace Nlog.API.Controllers\r\n{\r\n    [ApiController]\r\n    [Route(\"[controller]\")]\r\n    public class WeatherForecastController : ControllerBase\r\n    {\r\n\r\n        private readonly ILogger&lt;WeatherForecastController&gt; _logger;\r\n\r\n        public WeatherForecastController(ILogger&lt;WeatherForecastController&gt; logger)\r\n        {\r\n            _logger = logger;\r\n        }\r\n\r\n\r\n        [HttpGet]\r\n        public IEnumerable&lt;string&gt; Get()\r\n        {\r\n            _logger.LogDebug(\"This is a debug message\");\r\n            _logger.LogInformation(\"This is an info message\");  \r\n            _logger.LogWarning(\"This is a warning message \");\r\n            _logger.LogError(new Exception(), \"This is an error message\");\r\n\r\n            return new string[] { \"Cool\", \"Weather\" };\r\n        }\r\n    }\r\n}<\/pre>\n<p>Now let\u2019s launch our application and navigate to https:\/\/localhost:5001\/weatherforecast. <br \/>\nThe page displays an array of two strings: <code>Cool<\/code>, <code>Weather<\/code>.<\/p>\n<p>If everything is set up correctly, we should see the log messages stored in the <code>Logs<\/code> table:\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/07\/log-table.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-58851 size-full\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/07\/log-table.png\" alt=\"NLog logs table\" width=\"871\" height=\"221\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/07\/log-table.png 871w, https:\/\/code-maze.com\/wp-content\/uploads\/2021\/07\/log-table-300x76.png 300w, https:\/\/code-maze.com\/wp-content\/uploads\/2021\/07\/log-table-768x195.png 768w\" sizes=\"auto, (max-width: 871px) 100vw, 871px\" \/><\/a>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<\/p>\n<p>Log messages from <code>Microsoft.Hosting.Lifetime<\/code> are also stored because of the rule in our configuration file: \u00a0<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">&lt;logger name=\"*\" minlevel=\"Trace\" writeTo=\"dbTarget\" \/&gt;<\/pre>\n<p>The wildcard character (*) tells NLog to include all log messages including messages from Microsoft. If we want to skip all log messages from Microsoft and log only our messages, we can change the rule:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">&lt;logger name=\"Microsoft.*\" maxlevel=\"off\" final=\"true\" \/&gt;\r\n&lt;logger name=\"*\" minlevel=\"Trace\" writeTo=\"dbTarget\" \/&gt;<\/pre>\n<p>This rule will discard log messages from Microsoft.Hosting.Lifetime. However, according to the NLog <a href=\"https:\/\/github.com\/NLog\/NLog.Web\/wiki\/Hosting-Lifetime-Startup-Messages\" target=\"_blank\" rel=\"nofollow noopener\">documentation,<\/a> since we are using only NLog as our logging provider, discarding hosting lifetime startup messages can cause hosting environments such as Docker and Visual studio not to see our application as started.<\/p>\n<p>Lastly, if any error occurs during the setup, NLog logs its errors in the path specified in <code>internalLogFile<\/code> of our config file. This file helps in debugging.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>In this article, we have seen how to configure and use NLog to log messages into SQL Server. To log messages, we learned about creating a database target and using layout renderers provided by NLog as values for our table columns in SQL Server. We also saw how to configure an ASP.NET Core Web API project to use NLog.<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We have previously learned how to log messages to a File using NLog. In this article, we are going to learn how to use Nlog to log messages into SQL Server. We are going to see how we can configure an ASP.NET Core Web API project to support NLog and how we can configure NLog [&hellip;]<\/p>\n","protected":false},"author":31,"featured_media":59551,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[12],"tags":[586,41,930],"class_list":["post-58840","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-csharp","tag-asp-net-core-web-api","tag-nlog","tag-sql-server","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Writing Logs to SQL Server Using NLog - Code Maze<\/title>\n<meta name=\"description\" content=\"This article shows how to configure and use NLog to log messages to SQL Server database in an ASP.NET Core Web API Project.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Writing Logs to SQL Server Using NLog - Code Maze\" \/>\n<meta property=\"og:description\" content=\"This article shows how to configure and use NLog to log messages to SQL Server database in an ASP.NET Core Web API Project.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-06T05:00:25+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-22T10:55:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/09\/Writing-Logs-to-SQL-Server-Using-NLog.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Johnson Towoju\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Johnson Towoju\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/\"},\"author\":{\"name\":\"Johnson Towoju\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/2dcfa091e5a8b32c03ccb040f368c9d6\"},\"headline\":\"Writing Logs to SQL Server Using NLog\",\"datePublished\":\"2021-09-06T05:00:25+00:00\",\"dateModified\":\"2021-12-22T10:55:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/\"},\"wordCount\":871,\"commentCount\":7,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/09\/Writing-Logs-to-SQL-Server-Using-NLog.png\",\"keywords\":[\"asp.net core web api\",\"NLog\",\"SQL Server\"],\"articleSection\":[\"C#\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/\",\"url\":\"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/\",\"name\":\"Writing Logs to SQL Server Using NLog - Code Maze\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/09\/Writing-Logs-to-SQL-Server-Using-NLog.png\",\"datePublished\":\"2021-09-06T05:00:25+00:00\",\"dateModified\":\"2021-12-22T10:55:12+00:00\",\"description\":\"This article shows how to configure and use NLog to log messages to SQL Server database in an ASP.NET Core Web API Project.\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/09\/Writing-Logs-to-SQL-Server-Using-NLog.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/09\/Writing-Logs-to-SQL-Server-Using-NLog.png\",\"width\":1100,\"height\":620,\"caption\":\"Writing Logs to SQL Server Using NLog\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Writing Logs to SQL Server Using NLog\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/2dcfa091e5a8b32c03ccb040f368c9d6\",\"name\":\"Johnson Towoju\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2024\/02\/johnson-towoju-150x150.jpeg\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2024\/02\/johnson-towoju-150x150.jpeg\",\"caption\":\"Johnson Towoju\"},\"description\":\"Johnson Towoju is a full-stack .NET\/C# developer. He's enthusiastic and loves solving problems.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/johnson-towoju\/\"],\"url\":\"https:\/\/code-maze.com\/author\/johnson-towoju\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Writing Logs to SQL Server Using NLog - Code Maze","description":"This article shows how to configure and use NLog to log messages to SQL Server database in an ASP.NET Core Web API Project.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/","og_locale":"en_US","og_type":"article","og_title":"Writing Logs to SQL Server Using NLog - Code Maze","og_description":"This article shows how to configure and use NLog to log messages to SQL Server database in an ASP.NET Core Web API Project.","og_url":"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/","og_site_name":"Code Maze","article_published_time":"2021-09-06T05:00:25+00:00","article_modified_time":"2021-12-22T10:55:12+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/09\/Writing-Logs-to-SQL-Server-Using-NLog.png","type":"image\/png"}],"author":"Johnson Towoju","twitter_card":"summary_large_image","twitter_creator":"@CodeMazeBlog","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Johnson Towoju","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/"},"author":{"name":"Johnson Towoju","@id":"https:\/\/code-maze.com\/#\/schema\/person\/2dcfa091e5a8b32c03ccb040f368c9d6"},"headline":"Writing Logs to SQL Server Using NLog","datePublished":"2021-09-06T05:00:25+00:00","dateModified":"2021-12-22T10:55:12+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/"},"wordCount":871,"commentCount":7,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/09\/Writing-Logs-to-SQL-Server-Using-NLog.png","keywords":["asp.net core web api","NLog","SQL Server"],"articleSection":["C#"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/","url":"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/","name":"Writing Logs to SQL Server Using NLog - Code Maze","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/09\/Writing-Logs-to-SQL-Server-Using-NLog.png","datePublished":"2021-09-06T05:00:25+00:00","dateModified":"2021-12-22T10:55:12+00:00","description":"This article shows how to configure and use NLog to log messages to SQL Server database in an ASP.NET Core Web API Project.","breadcrumb":{"@id":"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/09\/Writing-Logs-to-SQL-Server-Using-NLog.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/09\/Writing-Logs-to-SQL-Server-Using-NLog.png","width":1100,"height":620,"caption":"Writing Logs to SQL Server Using NLog"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/writing-logs-to-sql-server-using-nlog\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"Writing Logs to SQL Server Using NLog"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/2dcfa091e5a8b32c03ccb040f368c9d6","name":"Johnson Towoju","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2024\/02\/johnson-towoju-150x150.jpeg","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2024\/02\/johnson-towoju-150x150.jpeg","caption":"Johnson Towoju"},"description":"Johnson Towoju is a full-stack .NET\/C# developer. He's enthusiastic and loves solving problems.","sameAs":["https:\/\/www.linkedin.com\/in\/johnson-towoju\/"],"url":"https:\/\/code-maze.com\/author\/johnson-towoju\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/58840","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=58840"}],"version-history":[{"count":41,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/58840\/revisions"}],"predecessor-version":[{"id":62082,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/58840\/revisions\/62082"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/59551"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=58840"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=58840"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=58840"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}