Msb.Logger
1.0.3
dotnet add package Msb.Logger --version 1.0.3
NuGet\Install-Package Msb.Logger -Version 1.0.3
<PackageReference Include="Msb.Logger" Version="1.0.3" />
<PackageVersion Include="Msb.Logger" Version="1.0.3" />
<PackageReference Include="Msb.Logger" />
paket add Msb.Logger --version 1.0.3
#r "nuget: Msb.Logger, 1.0.3"
#:package Msb.Logger@1.0.3
#addin nuget:?package=Msb.Logger&version=1.0.3
#tool nuget:?package=Msb.Logger&version=1.0.3
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-friendlyIAppLoggerfor 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 bySetSession()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 | Versions 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. |
-
.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.