Moongazing.SafeLog 1.0.0

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

Moongazing.SafeLog

Moongazing.SafeLog is a comprehensive exception handling and logging library designed for .NET applications. It centralizes exception management, provides robust logging capabilities using Serilog, and standardizes HTTP error responses.


Features

  • Global Exception Handling: Capture and handle exceptions across the application pipeline.
  • Custom Exception Types: Includes predefined exceptions for validation, authorization, business rules, and more.
  • Serilog Integration: Log exceptions and details to file, database, or other Serilog-supported sinks.
  • ProblemDetails Support: Standardized error responses for HTTP APIs.
  • Extensibility: Easily extend and customize exception handling and logging logic.

Installation

  1. Add the NuGet package:
    dotnet add package Moongazing.SafeLog
    
     Install Serilog and required sinks:
    
     dotnet add package Serilog
     dotnet add package Serilog.Sinks.File
     dotnet add package Serilog.Sinks.MSSqlServer
    
     Configure the middleware in your application.
    
    

Configuration appsettings.json Example

{ "SeriLogConfigurations": { "FileLogConfiguration": { "FolderPath": "logs/application", "FileSizeLimitBytes": 5000000 }, "MsSqlConfiguration": { "ConnectionString": "Server=localhost;Database=LogsDb;Trusted_Connection=True;", "TableName": "Logs", "AutoCreateSqlTable": true } } }

Middleware Setup

Register the exception middleware in Program.cs:

using Moongazing.SafeLog.Exceptions.Extensions;

var builder = WebApplication.CreateBuilder(args); var app = builder.Build();

app.ConfigureCustomExceptionMiddleware(); app.Run();

Usage Custom Exceptions

throw new ValidationException(new List<ValidationExceptionModel> { new ValidationExceptionModel { Property = "Email", Errors = new[] { "Email cannot be empty.", "Email must be valid." } } });

Logging Manually

var logger = new FileLogger(configuration); logger.Info("This is an informational log.");

Handling HTTP ProblemDetails

ValidationException returns:

{ "title": "Validation error(s)", "detail": "One or more validation errors occurred.", "status": 400, "type": "https://example.com/probs/validation", "errors": [ { "property": "Email", "errors": [ "Email cannot be empty.", "Email must be valid." ] } ] }

Architecture Exception Types Exception Type Description HTTP Status ValidationException Used for validation errors. 400 Bad Request NotFoundException Thrown when a resource is not found. 404 Not Found BusinessException Thrown for business rule violations. 400 Bad Request AuthorizationException Thrown for authorization failures. 401 Unauthorized Exception General exceptions. 500 Internal Server Error ProblemDetails

Predefined ProblemDetails classes for consistent HTTP error responses:

ValidationProblemDetails
NotFoundProblemDetails
BusinessProblemDetails
AuthorizationProblemDetails
InternalServerErrorProblemDetails

Extending Functionality Adding Custom Exceptions

Create a new exception class inheriting from Exception.
Override the appropriate HandleException method in HttpExceptionHandler.

Adding New Logging Sinks

Create a new LoggerService class.
Use Serilog to configure the new sink (e.g., Elasticsearch, Console).

Example:

public class ConsoleLogger : LoggerService { public ConsoleLogger() { Logger = new LoggerConfiguration().WriteTo.Console().CreateLogger(); } }

License

This project is licensed under the MIT License - see the LICENSE file for details. Contributing

We welcome contributions! Please fork the repository and submit a pull request with your improvements or bug fixes. Contact

For questions or support, feel free to open an issue or contact the maintainer:

Email: support@moongazing.dev
GitHub: Moongazing

Changelog v1.0.0

Initial release with core exception handling and logging functionality.
Added support for file and MSSQL logging.
Standardized HTTP ProblemDetails integration.
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

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.0 126 11/22/2024