Skip to content

Commit

Permalink
Merge pull request #32 from allegro/feature/error-handling-middleware…
Browse files Browse the repository at this point in the history
…-use-service-provider

feat: new `AddFluentErrorHandlingMiddleware` overload that allows to pass factories for logging methods
  • Loading branch information
starkpl authored Jan 12, 2024
2 parents 8f6b36f + 6dd6e7b commit 174e09c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@ public static IServiceCollection AddFluentErrorHandlingMiddleware(
Action<(string Message, Exception Exception)> logWarning,
Action<ErrorHandlingConfigurationBuilder>? customErrorHandlerRegistration = null,
IErrorSerializer? errorSerializer = null)
{
return services.AddFluentErrorHandlingMiddleware(
_ => logError,
_ => logWarning,
customErrorHandlerRegistration,
errorSerializer);
}

/// <summary>
/// Adds fluent error handling support to application
/// </summary>
/// <param name="services">Service collections</param>
/// <param name="logErrorFactory">Factory of delegate to log errors - enables to use any logger technology without Microsoft ILogger/<T/></param>
/// <param name="logWarningFactory">Factory of delegate to log warnings - enables to use any logger technology without Microsoft ILogger/<T/></param>
/// <param name="customErrorHandlerRegistration">Custom handler configurations</param>
/// <param name="errorSerializer">Enables to use other kind of error serialization than default System.Text.Json in Web mode</param>
public static IServiceCollection AddFluentErrorHandlingMiddleware(
this IServiceCollection services,
Func<IServiceProvider, Action<(string Message, Exception Exception)>> logErrorFactory,
Func<IServiceProvider, Action<(string Message, Exception Exception)>> logWarningFactory,
Action<ErrorHandlingConfigurationBuilder>? customErrorHandlerRegistration = null,
IErrorSerializer? errorSerializer = null)
{
var errorHandlingConfigurationBuilder = CreateErrorHandlingConfigurationBuilder(customErrorHandlerRegistration);

Expand All @@ -35,8 +57,8 @@ public static IServiceCollection AddFluentErrorHandlingMiddleware(
errorHandlingConfigurationBuilder.CustomErrorHandlerMap,
errorHandlingConfigurationBuilder.AdditionalInstrumentation,
errorSerializer ?? new SystemTextJsonWebErrorSerializer(),
logError,
logWarning));
logErrorFactory(provider),
logWarningFactory(provider)));
return services;
}

Expand Down
6 changes: 6 additions & 0 deletions src/Allegro.Extensions.AspNetCore/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.4.0] - 2024-01-11

### Added

* New `AddFluentErrorHandlingMiddleware` overload that allows to pass factories for logging methods.

## [1.3.0] - 2022-06-03

### Added
Expand Down
2 changes: 1 addition & 1 deletion src/Allegro.Extensions.AspNetCore/version.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<VersionPrefix>1.3.0</VersionPrefix>
<VersionPrefix>1.4.0</VersionPrefix>
</PropertyGroup>
</Project>

0 comments on commit 174e09c

Please sign in to comment.