DevBase.Logging 1.0.2

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

DevBase.Logging

DevBase.Logging is a lightweight logging library for .NET 9.0 that provides simple, type-safe logging with minimal configuration.

Features

  • Generic Type Logger - Type-safe logging with generic type parameter
  • Multiple Log Levels - INFO, WARNING, ERROR, DEBUG
  • Debug Output - Writes to Debug console
  • Timestamp Support - Automatic timestamp inclusion
  • Exception Logging - Built-in exception handling

Installation

dotnet add package DevBase.Logging

Quick Start

using DevBase.Logging.Logger;
using DevBase.Logging.Enums;

// Create logger with type
var logger = new Logger<MyClass>(this);

// Log messages
logger.Write("Application started", LogType.INFO);
logger.Write("Configuration loaded", LogType.DEBUG);
logger.Write("Connection timeout", LogType.WARNING);
logger.Write("Database error", LogType.ERROR);

// Log exceptions
try
{
    // ... operation ...
}
catch (Exception ex)
{
    logger.Write(ex);
}

Log Types

public enum LogType
{
    INFO,
    WARNING,
    ERROR,
    DEBUG
}

Usage Examples

Basic Logging

using DevBase.Logging.Logger;
using DevBase.Logging.Enums;

public class MyService
{
    private readonly Logger<MyService> _logger;
    
    public MyService()
    {
        _logger = new Logger<MyService>(this);
    }
    
    public void DoWork()
    {
        _logger.Write("Starting work", LogType.INFO);
        
        try
        {
            // Perform work
            _logger.Write("Work in progress", LogType.DEBUG);
        }
        catch (Exception ex)
        {
            _logger.Write(ex);
        }
        
        _logger.Write("Work completed", LogType.INFO);
    }
}

Logging in API Clients

using DevBase.Api.Apis.Deezer;
using DevBase.Logging.Logger;
using DevBase.Logging.Enums;

public class MusicService
{
    private readonly Logger<MusicService> _logger;
    private readonly Deezer _deezer;
    
    public MusicService()
    {
        _logger = new Logger<MusicService>(this);
        _deezer = new Deezer();
    }
    
    public async Task<JsonDeezerSearch> SearchAsync(string query)
    {
        _logger.Write($"Searching for: {query}", LogType.INFO);
        
        try
        {
            var results = await _deezer.Search(query);
            _logger.Write($"Found {results.data.Length} results", LogType.DEBUG);
            return results;
        }
        catch (Exception ex)
        {
            _logger.Write(ex);
            return null;
        }
    }
}

Logging HTTP Requests

using DevBase.Net.Core;
using DevBase.Logging.Logger;
using DevBase.Logging.Enums;

public class ApiClient
{
    private readonly Logger<ApiClient> _logger;
    
    public ApiClient()
    {
        _logger = new Logger<ApiClient>(this);
    }
    
    public async Task<Response> GetAsync(string url)
    {
        _logger.Write($"GET {url}", LogType.INFO);
        
        try
        {
            var response = await new Request(url).SendAsync();
            
            if (response.IsSuccessStatusCode)
            {
                _logger.Write($"Success: {response.StatusCode}", LogType.DEBUG);
            }
            else
            {
                _logger.Write($"Failed: {response.StatusCode}", LogType.WARNING);
            }
            
            return response;
        }
        catch (Exception ex)
        {
            _logger.Write(ex);
            throw;
        }
    }
}

Application Lifecycle Logging

public class Application
{
    private readonly Logger<Application> _logger;
    
    public Application()
    {
        _logger = new Logger<Application>(this);
    }
    
    public void Start()
    {
        _logger.Write("Application starting", LogType.INFO);
        
        try
        {
            InitializeComponents();
            _logger.Write("Components initialized", LogType.DEBUG);
            
            LoadConfiguration();
            _logger.Write("Configuration loaded", LogType.DEBUG);
            
            _logger.Write("Application started successfully", LogType.INFO);
        }
        catch (Exception ex)
        {
            _logger.Write(ex);
            _logger.Write("Application failed to start", LogType.ERROR);
            throw;
        }
    }
    
    public void Stop()
    {
        _logger.Write("Application stopping", LogType.INFO);
        
        try
        {
            CleanupResources();
            _logger.Write("Application stopped", LogType.INFO);
        }
        catch (Exception ex)
        {
            _logger.Write(ex);
        }
    }
}

Performance Monitoring

using DevBase.Extensions.Stopwatch;
using DevBase.Logging.Logger;
using DevBase.Logging.Enums;

public class PerformanceMonitor
{
    private readonly Logger<PerformanceMonitor> _logger;
    
    public PerformanceMonitor()
    {
        _logger = new Logger<PerformanceMonitor>(this);
    }
    
    public void MonitorOperation(Action operation, string name)
    {
        _logger.Write($"Starting: {name}", LogType.DEBUG);
        
        var stopwatch = Stopwatch.StartNew();
        
        try
        {
            operation();
            stopwatch.Stop();
            
            _logger.Write($"Completed: {name} in {stopwatch.Elapsed}", LogType.INFO);
        }
        catch (Exception ex)
        {
            stopwatch.Stop();
            _logger.Write($"Failed: {name} after {stopwatch.Elapsed}", LogType.ERROR);
            _logger.Write(ex);
            throw;
        }
    }
}

Database Operations

public class DatabaseService
{
    private readonly Logger<DatabaseService> _logger;
    
    public DatabaseService()
    {
        _logger = new Logger<DatabaseService>(this);
    }
    
    public async Task<User> GetUserAsync(int id)
    {
        _logger.Write($"Fetching user {id}", LogType.DEBUG);
        
        try
        {
            var user = await database.Users.FindAsync(id);
            
            if (user == null)
            {
                _logger.Write($"User {id} not found", LogType.WARNING);
            }
            else
            {
                _logger.Write($"User {id} retrieved", LogType.DEBUG);
            }
            
            return user;
        }
        catch (Exception ex)
        {
            _logger.Write(ex);
            throw;
        }
    }
}

Log Output Format

HH:mm:ss.fffffff : ClassName : LogType : Message

Example:

14:23:45.1234567 : MyService : INFO : Application started
14:23:45.2345678 : MyService : DEBUG : Configuration loaded
14:23:46.3456789 : MyService : ERROR : Connection failed

Best Practices

  1. Create logger per class - One logger instance per class
  2. Use appropriate log levels - INFO for important events, DEBUG for details
  3. Log exceptions - Always log exceptions with context
  4. Avoid sensitive data - Don't log passwords, tokens, or PII
  5. Be concise - Keep log messages short and informative

Common Patterns

Pattern 1: Try-Catch Logging

try
{
    PerformOperation();
}
catch (Exception ex)
{
    _logger.Write(ex);
    throw; // Re-throw if needed
}

Pattern 2: Conditional Logging

if (debugMode)
{
    _logger.Write("Debug information", LogType.DEBUG);
}

Pattern 3: Operation Tracking

_logger.Write("Starting operation", LogType.INFO);
// ... operation ...
_logger.Write("Operation completed", LogType.INFO);

Integration with DevBase Libraries

With DevBase.Net

var logger = new Logger<HttpClient>(this);

logger.Write("Sending request", LogType.DEBUG);
var response = await new Request(url).SendAsync();
logger.Write($"Response: {response.StatusCode}", LogType.INFO);

With DevBase.Api

var logger = new Logger<MusicClient>(this);

logger.Write("Searching Deezer", LogType.INFO);
var results = await deezer.Search(query);
logger.Write($"Found {results.data.Length} results", LogType.DEBUG);

With DevBase.Format

var logger = new Logger<LyricsParser>(this);

logger.Write("Parsing lyrics", LogType.DEBUG);
var lyrics = parser.ParseFromDisk("song.lrc");
logger.Write($"Parsed {lyrics.Length} lines", LogType.INFO);

Limitations

  • Debug output only - Logs to Debug console, not file or other sinks
  • No configuration - Fixed format and output
  • No filtering - All log levels are output
  • No async logging - Synchronous writes only

When to Use

Good for:

  • Simple applications
  • Debug logging during development
  • Quick prototyping
  • Internal tools

Not recommended for:

  • Production applications requiring file logging
  • Applications needing log aggregation
  • Systems requiring structured logging
  • High-performance scenarios

Alternatives

For production applications, consider:

  • Serilog - Structured logging with sinks
  • NLog - Flexible logging framework
  • Microsoft.Extensions.Logging - Built-in .NET logging

Target Framework

  • .NET 9.0

Dependencies

  • System.Diagnostics - Debug output

License

MIT License - See LICENSE file for details

Author

AlexanderDotH

Repository

https://github.com/AlexanderDotH/DevBase

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.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.2 178 12/24/2025
1.0.1 171 12/23/2025
1.0.0 319 4/13/2023