Msb.Logger 1.0.3

dotnet add package Msb.Logger --version 1.0.3
                    
NuGet\Install-Package Msb.Logger -Version 1.0.3
                    
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="Msb.Logger" Version="1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Msb.Logger" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="Msb.Logger" />
                    
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 Msb.Logger --version 1.0.3
                    
#r "nuget: Msb.Logger, 1.0.3"
                    
#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 Msb.Logger@1.0.3
                    
#: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=Msb.Logger&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=Msb.Logger&version=1.0.3
                    
Install as a Cake Tool

MsbLogger

A lightweight, flexible logging library for .NET applications supporting console output, file logging, and database persistence via PostgreSQL,MSSQL,MYSQL,SQLite. Works both with and without Dependency Injection.


Features

  • Dual usage modes — static (Logger) API for simple apps; DI-friendly IAppLogger for ASP.NET Core
  • Multiple sinks — console, file (CSV), and PostgreSQL database
  • Selective DB logging — choose which log levels persist to the database
  • Session tagging — attach a session identifier to any log entry
  • Queue-based logging — optional async buffered writes for high-throughput scenarios
  • Structured CSV output — timestamped logs with caller context (area, method, line)

Manual Table Creation

Msb.Logger automatically creates the application_logs table when the application starts. If automatic table creation fails (for example, due to insufficient database permissions), create the table manually using the schema below.

Column Name Data Type Description
id Primary Key / Identity Unique log identifier
log_date_time String Date and time of the log entry
area String Application area or module
method_name String Method where the log was generated
line_number Integer Source code line number
log_type String Information, Warning, Error, Debug, etc.
message String Log message
session String Session or correlation identifier

Log Levels

LogType Description
Information General informational messages
Warning Warnings or potential issues
Error Errors or exceptions
Debug Debugging and development details
Critical Critical errors requiring immediate attention
Trace Fine-grained trace events

Getting Started

Without Dependency Injection

Suitable for console apps, Windows Forms, WPF, or any non-DI context.

Logger.Configure(builder =>
{
    builder
        .UseDatabase(() => new NpgsqlConnection(
            "server=localhost;port=5432;database=my_app;user id=postgres;password=***"))
        .AddConsole()
        .WithSessionClearing()        // clears session data after each log entry
        .SetLogDirectory("Logs")
        .EnableDbLevels(LogType.Critical, LogType.Error);
        // .EnableQueueLogger();      // enable async queue; exit
});

Logger.SetSession("MySession");
Logger.Log("This is an error message", LogType.Error);

Logger.SetSession("MySession");
Logger.Log("This is a critical message", LogType.Critical);

Logger.Info("This is an info message");
Logger.Error("This is an error message");

try
{
    // your code
}
catch (Exception ex)
{
    Logger.Log(ex);
}

With Dependency Injection (ASP.NET Core)

Register IAppLogger in Program.cs or Startup.cs:

builder.Services.AddScoped<IAppLogger>(sp =>
    new MsbLoggerBuilder()
        .Create()
        .UseDatabase(() => new NpgsqlConnection(
            "server=localhost;port=5432;database=my_app;user id=postgres;password=***"))
        .EnableDbLevels(LogType.Error, LogType.Critical)
        .AddConsole()
        .WithSessionClearing()
       // .EnableQueueLogger()    // enable async queue;
        .Build());

Inject and use in your classes:

public class MyService
{
    private readonly IAppLogger _logger;

    public MyService(IAppLogger logger)
    {
        _logger = logger;
    }

    public void DoWork()
    {
        _logger.SetSession("MySession");
        _logger.Log("This is an error message", LogType.Error);

        _logger.SetSession("MySession");
        _logger.Log("This is a critical message", LogType.Critical);

        _logger.Info("This is an info message");
        _logger.Error("This is an error message");

        try
        {
            // your code
        }
        catch (Exception ex)
        {
            _logger.Log(ex);
        }
    }
}

Configuration Options

Method Description
.UseDatabase(factory) Provides a DB(PostgreSQL,MSSQL,MYSQL,SQLite) factory for database logging
.AddConsole() Enables console output (also works for ASP.NET Core logging pipeline)
.WithSessionClearing() Clears the session identifier after each log write; omit to retain session for the app lifetime
.SetLogDirectory(path) Directory where CSV log files are written (e.g., "Logs")
.EnableDbLevels(params types) Restricts database writes to the specified LogType values
.EnableQueueLogger() Enables async buffered writes

Convenience Methods

Both the static Logger and IAppLogger expose shorthand methods:

Logger.Info("message");    // LogType.Information
Logger.Error("message");   // LogType.Error
Logger.Log(exception);     // logs exception details automatically
Logger.Log("message", LogType.Warning);  // explicit level

CSV Log Format

Logs written to file use the following CSV structure:

DateTime,Area,Method,Line,Type,Message,Session
"2026-06-14 00:15:23.639","Form1",".ctor",18,"Information","hello",""
"2026-06-14 00:16:22.513","Form1",".ctor",18,"Information","hello",""
Column Description
DateTime Timestamp of the log entry (yyyy-MM-dd HH:mm:ss.fff)
Area Class or form name where the log was called
Method Method name (e.g., .ctor for constructors)
Line Line number in source file
Type Log level (Information, Error, Critical, etc.)
Message The log message text
Session Session identifier, or empty if not set / cleared

Log files are stored in the directory specified by .SetLogDirectory() (default: Logs/).


Important Notes

  • Queue logger flush — when .EnableQueueLogger() is active` before your application exits to ensure all buffered log entries are written to the database.
  • Session lifecycle.WithSessionClearing() resets the session after every log call. Omitting it keeps the session value set by SetSession() for the entire application lifetime.
  • DB level filtering — only the levels passed to .EnableDbLevels() are written to PostgreSQL; all levels still write to the console and file sinks.
  • Credentials — avoid hard-coding database passwords in source code; prefer environment variables or secrets management.

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.
  • .NETStandard 2.0

    • No dependencies.

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.3 98 6/15/2026
1.0.1 100 6/14/2026
1.0.0 98 6/14/2026