Daenet.Common.Logging.Sql 1.1.0

dotnet add package Daenet.Common.Logging.Sql --version 1.1.0
                    
NuGet\Install-Package Daenet.Common.Logging.Sql -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Daenet.Common.Logging.Sql" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Daenet.Common.Logging.Sql" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Daenet.Common.Logging.Sql" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Daenet.Common.Logging.Sql --version 1.1.0
                    
#r "nuget: Daenet.Common.Logging.Sql, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Daenet.Common.Logging.Sql@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Daenet.Common.Logging.Sql&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Daenet.Common.Logging.Sql&version=1.1.0
                    
Install as a Cake Tool

SQL Logger

Implementation of Logging in SQL Server Database for Dot Net Core Applications.

This repository contains Implementation of ASP.NET Core Logger Provider in SQL Server Database. It enables you to log the information in SQL Database table. For general ASP.NET Core logger implementation, visit here.

Installation

Install the Daenet.Common.Logging.Sql NuGet Package in your application.

Configuration

The SqlServerLogger needs to be configured and initialized.

ASP.NET Core Web Application

public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
            .ConfigureLogging((hostingContext, logging) =>
            {
                var loggerSection = hostingContext.Configuration.GetSection("Logging");
                logging.AddConfiguration(loggerSection);
                logging.AddConsole();
                logging.AddDebug();
                logging.AddSqlServerLogger((sett) =>
                {
                    sett.SetSqlServerLoggerSettings(loggerSection);
                });
            })
                .Build();
    }

Console Application

  public IConfigurationRoot Configuration;
  var builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
  Configuration = builder.Build();

  ILoggerFactory loggerFactory = new LoggerFactory().AddSqlServerLogger(Configuration.GetSection("Logging"));
  ILogger logger = loggerFactory.CreateLogger<SqlServerLoggerTests>();

In the appsettings.json, the SqlProvider part needs to be added to Logging.

{
  "Logging": {
    "IncludeScopes": true,
    "Debug": {
      "LogLevel": {
        "Default": "Information"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "SqlProvider": {
      "LogLevel": {
        "Default": "Information"
      },
      "ConnectionString": "",
      "TableName": "SqlLog",
      "BatchSize": 1,
      "InsertTimerInSec": 60,
      "IncludeExceptionStackTrace": false,
      "IgnoreLoggingErrors": false,
      "ScopeSeparator": "=>"
    }
  }
}

LogLevel configuration are done on global and logger level see Introduction to Logging in ASP.NET Core

IncludeScope flag is used when the logging is to be done inside one particular scope. IncludeScope when set to true, and initialized inside code with beginScope("scopeStatement") adds scope id to every logged statement which is added to database under column "Scope". Following example illustrates using scope in logging when IncludeScopes flag is set to true.

 using (m_Logger.BeginScope($"Scope Begins : {Guid.NewGuid()}"))
 {
   //Here log something
 }

This will add "Scope begins : new hexadecimal guid" for every object instance of the class where scope is began.

And to persist the scope value to the database, you must map SCOPEPATH to the corresponding column name using the ScopeColumnMapping configuration:

"ScopeColumnMapping": {
        "SCOPEPATH": "Scope"
      }

ConnectionString is ADO.NET connection string for SQL authentication.

TableName is name of the table where logger should log.

BatchSize Decides when to write messages to the database. If BatchSize > 1 then we fill a List and wait till list count reaches BatchSize or Insert to DB is triggered by InsertTimerInSec. If BatchSize > 1 writing the data to db is async.

InsertTimerInSec Time elapsed when logger writes to table even BatchSize isn't reached.

(REMOVED in 1.0.7.2) CreateTblIfNotExist flag when set to true, gives the logger ability to create the table of table name that is provided in configuration in case it is not already available in database. This flag is useful while testing the logger in development environment.

IncludeExceptionStackTrace flag is currently not implemented and complete exception is logged in table regardless of this flag.

IgnoreLoggingErrors flag is to decide if the application should fail if an exception occurs on logging. When set to false, the logger at the moment logs these errors in debug console. **WARNING: Only works if BatchSize = 1

Database Configuration

To log the error in SQL database, A table must be present in database with predefined column format. Following columns are filled by logger -

  • Id - Unique id of each entry, automatically incremented
  • EventId - Event Id of each log.
  • Type - Log level
  • Scope - Scope information if scoping is enabled and used
  • Message - Log message
  • Exception - Complete exception in case of error
  • TimeStamp - Timestamp of log
  • CategoryName - Namespace from where the log is logged

Following query will create a new table in SQL Database with above parameters, and should be used as a reference query for default format.

CREATE TABLE [dbo].[YourTableName](
       [Id] [bigint] IDENTITY(1,1) NOT NULL,
       [EventId] [int] NULL,
       [Type] [nvarchar](15) NOT NULL,
       [Scope] [nvarchar](MAX) NULL,
       [Message] [nvarchar](max) NOT NULL,
       [Exception] [nvarchar](max) NULL,
       [TimeStamp] [datetime] NOT NULL,
       [CategoryName] [nvarchar] (max) NULL,
CONSTRAINT [PK_YourTableName] PRIMARY KEY CLUSTERED 
(
       [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

Logging Parameters to database

It's possible to log parameters to database, too. In this example we want to log the RequestId and the scope to the database. For this to work, you have to create the columns of course.

This configuration has to be apppended to the SqlProvider section in Logging.

Here is an example of how to log the 2 .NET Core internal logging parameters. You can extend this, with the parameters you use in your logging. A good example here is {method} for logging the method name in a general way.

"ScopeColumnMapping": {
        "RequestId": "RequestId",
        "SCOPEPATH": "Scope"
      }

Custom SQL Logging Format

It is also possible to use your own custom logging format by providing loggingFormatter

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 2,613 12/10/2025
1.0.11 11,790 1/23/2024
1.0.10.3 4,159 1/27/2022
1.0.10.2 668 1/26/2022
1.0.9.1 4,175 7/21/2021
1.0.8.7 3,531 3/18/2021
1.0.8.6 1,032 2/22/2021
1.0.8.5 794 1/14/2021
1.0.8.4 1,022 9/30/2020
1.0.8.3 862 9/24/2020
1.0.8.2 833 8/18/2020
1.0.7.2 766 7/31/2020
1.0.7.1-preview 663 4/30/2020
1.0.6 1,841 5/24/2018
1.0.5 1,551 11/29/2017
1.0.4 1,340 11/29/2017
1.0.3 1,362 11/29/2017
1.0.2 1,323 11/29/2017
1.0.1 1,364 10/20/2017
0.0.5 1,711 9/26/2017
Loading failed