DainnCommon 9.0.2
dotnet add package DainnCommon --version 9.0.2
NuGet\Install-Package DainnCommon -Version 9.0.2
<PackageReference Include="DainnCommon" Version="9.0.2" />
<PackageVersion Include="DainnCommon" Version="9.0.2" />
<PackageReference Include="DainnCommon" />
paket add DainnCommon --version 9.0.2
#r "nuget: DainnCommon, 9.0.2"
#:package DainnCommon@9.0.2
#addin nuget:?package=DainnCommon&version=9.0.2
#tool nuget:?package=DainnCommon&version=9.0.2
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 violatedNotFoundException- Thrown when requested resources are not foundValidationException- 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 dayIDailyFileLogger- Interface for daily file loggingDailyFileLoggerOptions- 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 | Versions 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. |
-
net9.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
- Microsoft.EntityFrameworkCore (>= 9.0.0)
- Microsoft.EntityFrameworkCore.InMemory (>= 9.0.0)
- Microsoft.EntityFrameworkCore.Sqlite (>= 9.0.0)
- Microsoft.EntityFrameworkCore.SqlServer (>= 9.0.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 9.0.0)
- Microsoft.Extensions.Configuration.Json (>= 9.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.0)
- Npgsql.EntityFrameworkCore.PostgreSQL (>= 9.0.0)
- Pomelo.EntityFrameworkCore.MySql (>= 9.0.0)
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.