Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More work on HttpResultError logging #115

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>3.0.0</Version>
<Version>3.0.1</Version>
<Description>A library contains common code related to functional programming in ASP.NET Core based on LanguageExt.</Description>
<PackageProjectUrl>https://github.com/codehardth/Codehard.Functional</PackageProjectUrl>
<RepositoryUrl>https://github.com/codehardth/Codehard.Functional</RepositoryUrl>
<PackageReleaseNotes>Add error wrapper action result loggin filter</PackageReleaseNotes>
<PackageReleaseNotes>More works on error wrapper action result logging</PackageReleaseNotes>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Codehard.Functional.AspNetCore;

/// <summary>
/// Provides extension methods for converting functional results into ASP.NET Core action results.
/// </summary>
public static class ControllerExtensions
{
private static IActionResult MapToActionResult<T>(HttpStatusCode statusCode, T result)
Expand Down Expand Up @@ -50,7 +53,18 @@ public static IActionResult MatchToResult<T>(
return fin
.Match(
res => MapToActionResult(successStatusCode, res),
MapErrorToActionResult);
err =>
{
switch (err)
{
case HttpResultError hre:
logger?.Log(hre);
return MapErrorToActionResult(hre);
default:
logger?.Log(err);
return MapErrorToActionResult(err);
}
});
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ private Task OnActionExecutedAsync(ActionExecutedContext context)
case ErrorWrapperActionResult ewar:
LogHttpResultError(ewar.Error);
break;

case IStatusCodeActionResult statusCodeResult:
{
if(statusCodeResult.StatusCode == StatusCodes.Status500InternalServerError)
{
LogError(context.Exception);
Log(context.Exception);
}

break;
Expand All @@ -57,7 +58,16 @@ private Task OnActionExecutedAsync(ActionExecutedContext context)

void LogHttpResultError(HttpResultError error)
{
this.logger.LogError(
var logLevel =
error.StatusCode switch
{
>= HttpStatusCode.InternalServerError => LogLevel.Error,
>= HttpStatusCode.BadRequest => LogLevel.Warning,
_ => LogLevel.Information
};

this.logger.Log(
logLevel: logLevel,
message: "TraceId: {TraceId}, {Path}, {Query}, {Method}, {ResponseStatus}, {ErrorCode}",
traceId,
Sanitize(context.HttpContext.Request.Path),
Expand All @@ -74,16 +84,25 @@ void LogErrorOpt(Option<Error> errorOpt)
errorOpt.Iter(
Some: error =>
{
LogError(
Log(
exception: error.Exception.IfNoneUnsafe(default(Exception)));

LogErrorOpt(error.Inner);
});
}

void LogError(Exception? exception)
void Log(Exception? exception)
{
this.logger.LogError(
var logLevel =
exception switch
{
InvalidOperationException => LogLevel.Warning,
default(Exception) => LogLevel.Information,
_ => LogLevel.Error,
};

this.logger.Log(
logLevel: logLevel,
exception: exception,
message: "TraceId: {TraceId}, {Path}, {Query}, {Method}, {ResponseStatus}",
traceId,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Codehard.Functional.Logger;

namespace Codehard.Functional.AspNetCore;

/// <summary>
/// Contains extension methods for logging HTTP result errors with an <see cref="ILogger"/> instance.
/// </summary>
public static class LoggingExtensions
{
/// <summary>
/// Logs the specified HTTP result error using the provided logger.
/// </summary>
/// <param name="logger">The logger to use for logging the error.</param>
/// <param name="error">The HTTP result error to log.</param>
public static void Log(this ILogger logger, HttpResultError error)
{
switch (error.StatusCode)
{
case >= HttpStatusCode.InternalServerError:
logger.LogError(
exception: error.Exception.IfNoneUnsafe(default(Exception)),
message: "{ResponseStatus}, {ErrorCode}, {Message}",
error.StatusCode,
error.ErrorCode.IfNoneUnsafe(default(string)),
error.Message);

error.Inner.Do(
err =>
logger.Log(err, logLevel: LogLevel.Error));
break;

case >= HttpStatusCode.BadRequest:
logger.LogWarning(
exception: error.Exception.IfNoneUnsafe(default(Exception)),
message: "{ResponseStatus}, {ErrorCode}, {Message}",
error.StatusCode,
error.ErrorCode.IfNoneUnsafe(default(string)),
error.Message);

error.Inner.Do(
err =>
logger.Log(err, logLevel: LogLevel.Warning));
break;
}
}
}
Loading