Moongazing.SafeLog
1.0.0
dotnet add package Moongazing.SafeLog --version 1.0.0
NuGet\Install-Package Moongazing.SafeLog -Version 1.0.0
<PackageReference Include="Moongazing.SafeLog" Version="1.0.0" />
<PackageVersion Include="Moongazing.SafeLog" Version="1.0.0" />
<PackageReference Include="Moongazing.SafeLog" />
paket add Moongazing.SafeLog --version 1.0.0
#r "nuget: Moongazing.SafeLog, 1.0.0"
#:package Moongazing.SafeLog@1.0.0
#addin nuget:?package=Moongazing.SafeLog&version=1.0.0
#tool nuget:?package=Moongazing.SafeLog&version=1.0.0
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
- 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 | 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.AspNetCore.Mvc.Core (>= 2.2.5)
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.0)
- Serilog (>= 4.1.0)
- Serilog.Sinks.File (>= 6.0.0)
- Serilog.Sinks.MSSqlServer (>= 8.0.0)
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 |