-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
12 changed files
with
72 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
49 changes: 15 additions & 34 deletions
49
src/NKZSoft.Template.Common/Extensions/LoggerExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,25 @@ | ||
namespace NKZSoft.Template.Common.Extensions; | ||
|
||
internal static class LoggerExtension | ||
public static partial class LoggerExtension | ||
{ | ||
private static readonly Action<ILogger, string, Exception?> _consumeIntegrationEvent = LoggerMessage.Define<string>( | ||
LogLevel.Information, | ||
EventIds.ConsumeIntegrationEvent, | ||
"Integration event has been consumed: `{Message}`."); | ||
[LoggerMessage(1, LogLevel.Information, "Integration event has been consumed: {Message}.")] | ||
internal static partial void ConsumeIntegrationEvent(this ILogger logger, string message); | ||
|
||
private static readonly Action<ILogger, string, Exception?> _raiseIntegrationEvent = LoggerMessage.Define<string>( | ||
LogLevel.Information, | ||
EventIds.RaiseIntegrationEvent, | ||
"Domain event has been raised: `{Message}`."); | ||
[LoggerMessage(2, LogLevel.Information, "Domain event has been raised: {Message}.")] | ||
public static partial void RaiseIntegrationEvent(this ILogger logger, string message); | ||
|
||
private static readonly Action<ILogger, long, string, Exception?> _longRunningRequest = LoggerMessage.Define<long, string>( | ||
LogLevel.Warning, | ||
EventIds.LongRunningRequest, | ||
"Long running request: `{ElapsedMilliseconds}` milliseconds `{Request}.`"); | ||
[LoggerMessage(3, LogLevel.Warning, "Long running request: {ElapsedMilliseconds} milliseconds {Request}.")] | ||
public static partial void LongRunningRequest(this ILogger logger, long elapsedMilliseconds, string request); | ||
|
||
private static readonly Action<ILogger, string?, Exception?> _unhandledExceptionRequest = LoggerMessage.Define<string?>( | ||
LogLevel.Error, | ||
EventIds.UnhandledExceptionRequest, | ||
"Unhandled exception has occured for request: `{Request}.`"); | ||
[LoggerMessage(4, LogLevel.Error, "Unhandled exception has occured for request: `{Request}.")] | ||
public static partial void UnhandledExceptionRequest(this ILogger logger, string? request); | ||
|
||
private static readonly Action<ILogger, string?, Exception?> _loggingRequest = LoggerMessage.Define<string?>( | ||
LogLevel.Error, | ||
EventIds.LoggingRequest, | ||
"Request has executed: `{Request}.`"); | ||
[LoggerMessage(5, LogLevel.Error, "Request has executed: `{Request}.")] | ||
public static partial void LoggingRequest(this ILogger logger, string? request); | ||
|
||
public static void ConsumeIntegrationEvent(this ILogger logger, string message) | ||
=> _consumeIntegrationEvent(logger, message, null); | ||
[LoggerMessage(6, LogLevel.Error, "An error occurred while migrating or initializing the database.")] | ||
public static partial void MigrationError(this ILogger logger, Exception ex); | ||
|
||
public static void RaiseIntegrationEvent(this ILogger logger, string message) | ||
=> _raiseIntegrationEvent(logger, message, null); | ||
|
||
public static void LongRunningRequest(this ILogger logger, long elapsedMilliseconds, string request) | ||
=> _longRunningRequest(logger, elapsedMilliseconds, request, null); | ||
|
||
public static void UnhandledExceptionRequest(this ILogger logger, string? request, Exception? ex) | ||
=> _unhandledExceptionRequest(logger, request, ex); | ||
|
||
public static void LoggingRequest(this ILogger logger, string? request) | ||
=> _loggingRequest(logger, request, null); | ||
[LoggerMessage(7, LogLevel.Error, "Application: An unhandled exception has occurred.")] | ||
public static partial void ApplicationUnhandledException(this ILogger logger, Exception ex); | ||
} |
14 changes: 7 additions & 7 deletions
14
src/NKZSoft.Template.Presentation.Rest/Extensions/ApplicationBuilderExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/NKZSoft.Template.Presentation.Rest/Middleware/GlobalExceptionHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
namespace NKZSoft.Template.Presentation.Rest.Middleware; | ||
|
||
using Common.Extensions; | ||
|
||
public class GlobalExceptionHandler(ILogger<GlobalExceptionHandler> logger) : IExceptionHandler | ||
{ | ||
public async ValueTask<bool> TryHandleAsync(HttpContext httpContext, Exception exception, CancellationToken cancellationToken) | ||
{ | ||
logger.ApplicationUnhandledException(exception); | ||
|
||
var problemDetails = new ProblemDetails { Instance = httpContext.Request.Path }; | ||
if (exception is ValidationException fluentException) | ||
{ | ||
problemDetails.Title = "one or more validation errors occurred."; | ||
problemDetails.Type = "https://tools.ietf.org/html/rfc7231#section-6.5.1"; | ||
httpContext.Response.StatusCode = StatusCodes.Status400BadRequest; | ||
var validationErrors = | ||
fluentException.Errors.Select(error => error.ErrorMessage).ToList(); | ||
problemDetails.Extensions.Add("errors", validationErrors); | ||
} | ||
else | ||
{ | ||
problemDetails.Title = exception.Message; | ||
} | ||
|
||
problemDetails.Status = httpContext.Response.StatusCode; | ||
await httpContext.Response.WriteAsJsonAsync(problemDetails, cancellationToken).ConfigureAwait(false); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters