-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor LoggingBehaviour and ValidationBehaviour classes
Removed unnecessary fields and constructors from both LoggingBehaviour and ValidationBehaviour classes, streamlining them for better readability and performance. Adjusted method implementations to directly use passed parameters instead of stored fields.
- Loading branch information
Gary Woodfine
committed
Jan 16, 2024
1 parent
714fae6
commit a42cd61
Showing
4 changed files
with
17 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,29 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using MediatR; | ||
using Serilog; | ||
using ILogger = Serilog.ILogger; | ||
|
||
namespace ApiProject.Behaviours | ||
{ | ||
public class LoggingBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> | ||
public class LoggingBehaviour<TRequest, TResponse>(ILogger logger) : IPipelineBehavior<TRequest, TResponse> | ||
where TRequest : IRequest<TResponse> | ||
{ | ||
private readonly ILogger _logger; | ||
public LoggingBehaviour(ILogger logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
|
||
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken) | ||
{ | ||
//Query | ||
_logger.Information($"Handling {typeof(TRequest).Name}"); | ||
logger.Information($"Handling {typeof(TRequest).Name}"); | ||
Type myType = request.GetType(); | ||
IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties()); | ||
foreach (PropertyInfo prop in props) | ||
{ | ||
object propValue = prop.GetValue(request, null); | ||
_logger.Information("{Property} : {@Value}", prop.Name, propValue); | ||
logger.Information("{Property} : {@Value}", prop.Name, propValue); | ||
} | ||
var response = await next(); | ||
//FilterResponse | ||
_logger.Information($"Handled {typeof(TResponse).Name}"); | ||
logger.Information($"Handled {typeof(TResponse).Name}"); | ||
return response; | ||
} | ||
} | ||
|
||
|
||
} |
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