DainnCommon 9.0.2

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

DainnCommon

A shared library containing common functionality used across Dainn projects.

Overview

This library provides shared infrastructure components that are used by multiple Dainn projects, including:

  • Exception handling classes
  • Middleware components
  • Database provider configuration helpers
  • Entity Framework Core extensions

Components

Exceptions

Common exception classes for consistent error handling:

  • BusinessRuleException - Thrown when business rules are violated
  • NotFoundException - Thrown when requested resources are not found
  • ValidationException - Thrown when input validation fails

Middleware

  • ExceptionMiddleware - Global exception handling middleware that formats exceptions into consistent JSON responses

Extensions

  • DbContextProviderExtensions - Extension methods for configuring database providers (SQLite, SQL Server, PostgreSQL, MySQL, InMemory)
  • PostgreSQL UUID configuration helpers

Data

  • DbContextFactoryHelper - Helper methods for creating DbContext instances at design time

Logging

  • DailyFileLogger - Thread-safe file logger that creates separate log files for each day
  • IDailyFileLogger - Interface for daily file logging
  • DailyFileLoggerOptions - Configuration options for daily file logging
  • Log files are automatically organized by date (e.g., app-2025-12-25.log)
  • Automatic log file rotation and cleanup based on retention policy

Usage

Adding to Your Project

Add a project reference to DainnCommon:

<ProjectReference Include="..\DainnCommon\DainnCommon.csproj" />

Using Exceptions

using DainnCommon.Exceptions;

throw new NotFoundException("User", userId);
throw new BusinessRuleException("Cannot delete active user");
throw new ValidationException(validationErrors);

Using Exception Middleware

using DainnCommon.Middleware;

app.UseMiddleware<ExceptionMiddleware>();

Configuring Database Providers

using DainnCommon.Extensions;

services.AddDbContext<MyDbContext>(options =>
{
    options.ConfigureDatabaseProvider(provider, connectionString);
});

Configuring PostgreSQL UUIDs

using DainnCommon.Extensions;

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    if (Database.IsNpgsql())
    {
        modelBuilder.ConfigurePostgreSqlUuids(setDefaultValue: true);
    }
}

Using DbContext Factory Helper

using DainnCommon.Data;

public class MyDbContextFactory : IDesignTimeDbContextFactory<MyDbContext>
{
    public MyDbContext CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder<MyDbContext>();
        var configuration = DbContextFactoryHelper.BuildConfiguration();
        var (provider, connectionString) = DbContextFactoryHelper.GetDatabaseConfiguration(configuration);
        DbContextFactoryHelper.ConfigureDbContext(optionsBuilder, provider, connectionString);
        return new MyDbContext(optionsBuilder.Options);
    }
}

Using Daily File Logger

Configuration in appsettings.json
{
  "DailyFileLogging": {
    "LogDirectory": "logs",
    "LogFilePrefix": "app",
    "MinimumLogLevel": "Information",
    "IncludeTimestamp": true,
    "TimestampFormat": "yyyy-MM-dd HH:mm:ss.fff",
    "IncludeLogLevel": true,
    "RetentionDays": 30,
    "CreateDirectoryIfNotExists": true
  }
}
Registering the Logger
using DainnCommon.Extensions;

// With configuration
builder.Services.AddDailyFileLogging(builder.Configuration);

// Or with explicit options
builder.Services.AddDailyFileLogging(options =>
{
    options.LogDirectory = "logs";
    options.LogFilePrefix = "myapp";
    options.MinimumLogLevel = LogLevel.Information;
    options.RetentionDays = 30;
});
Using the Logger
using DainnCommon.Logging;

public class MyService
{
    private readonly IDailyFileLogger _logger;

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

    public void DoSomething()
    {
        _logger.LogInformation("Operation started");
        
        try
        {
            // Your code here
            _logger.LogInformation("Operation completed successfully");
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Operation failed");
        }
    }
}
Log File Format

Log files are created with the format: {LogFilePrefix}-{yyyy-MM-dd}.log

Example: app-2025-12-25.log

Each log entry includes:

  • Timestamp (if enabled)
  • Log level (if enabled)
  • Message
  • Exception details (if provided)

Supported Database Providers

  • SQLite
  • SQL Server
  • PostgreSQL / Npgsql
  • MySQL / MariaDB
  • InMemory

Requirements

  • .NET 8.0 or later
  • Entity Framework Core 8.0.11 or later
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.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on DainnCommon:

Package Downloads
DainnUser.PostgreSQL

Extensible user management for ASP.NET Core APIs

DainnProductEAV.PostgreSQL

A .NET 9 class library implementing the Entity-Attribute-Value (EAV) pattern for product catalog management. Provides a flexible, extensible product catalog system that allows adding custom attributes without modifying the database schema.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
9.0.2 176 1/8/2026
9.0.0 249 12/25/2025