anjosoft.dblogger 1.0.0

Prefix Reserved
dotnet add package anjosoft.dblogger --version 1.0.0                
NuGet\Install-Package anjosoft.dblogger -Version 1.0.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="anjosoft.dblogger" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add anjosoft.dblogger --version 1.0.0                
#r "nuget: anjosoft.dblogger, 1.0.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.
// Install anjosoft.dblogger as a Cake Addin
#addin nuget:?package=anjosoft.dblogger&version=1.0.0

// Install anjosoft.dblogger as a Cake Tool
#tool nuget:?package=anjosoft.dblogger&version=1.0.0                

DBLogger

v.1.0.0

Logging provider for .net that allows you to log messages to Microsoft SQL and Azure SQL databases

Installation

DBLogger is hosted at nuget.org. To Install, run: dotnet add package anjosoft.dblogger


Usage

import the anjosoft.dblogger namespace

Configure your log levels, set your SQL connection string, and specify your log table. For example:

  "Logging": {
    "Database": {
      "Options": {
          "ConnectionString": "Server=MyServer;Database=MyDatabase;User Id=user;Password=UserPassword;TrustServerCertificate=True;",
          "LogTable": "dbo.ApplicationLog"
      },
      "LogLevel": {
          "Default": "Information",
          "Microsoft": "Error"
      }
    }
  },
  "AllowedHosts": "*"
}

Add DBLogger and configure your logger options (Program.cs):

builder.Logging.AddDbLogger(options =>
{
    builder.Configuration.GetSection("Logging").GetSection("Database").GetSection("Options").Bind(options);
});

Create Log Table

Your application users should only have Read, Write, and Execute privileges on SQL, therefore, DBLogger should not be allowed to create the log table for you. Create a table with the following columns:

EventId - Integer (Application error id)
LogLevel - String (Indicates the severity of the log)
EventName - String (Name of the event that triggered the error)
Message - String (Main description of the event being logged)
ExceptionMessage - String (The description of the exception itself, generated by the .NET runtime or the code that threw the exception)
ExceptionStackTrace - String (Stack Trace associated with the exception)
ExceptionSource - String (Identifies the assembly or code module where the exception originated)
DateTime - Nullable DateTime (Date and Time the message was generated)

Example:

create table dbo.ApplicationLog
(
    [Id] int not null identity(1, 1),
    [EventId] int null,
    [LogLevel] varchar(16),
    [EventName] varchar(128),
    [Message] nvarchar(1024),
    [ExceptionMessage] nvarchar(1024),
    [ExceptionStackTrace] nvarchar(max),
    [ExceptionSource] nvarchar(64),
    [DateTime] DateTime2,
    index IX_AppLog nonclustered ([LogLevel], [EventName], DateTime)
)

Example

...
using anjosoft.dblogger;

namespace myapp_api.Controllers
{
    [Route("items")]
    public class ItemsController(IItemService itemService, ILogger<DBLogger> logger) : ControllerBase
    {
        private readonly IItemService itemService = itemService;
        private readonly ILogger<DBLogger> logger = logger;

        [HttpGet("{id}")]
        public async Task<IActionResult> Get(int id)
        {
            var item = await itemService.GetItemAsync(id);

            //logger.LogInformation("test");  // example no. 1            
            //throw new Exception("test"); // example no. 2
            ...
        }
        ...
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
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.0.0 155 3/20/2024