-
-
Notifications
You must be signed in to change notification settings - Fork 151
Logging Behavior #15
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
base: master
Are you sure you want to change the base?
Logging Behavior #15
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| using AutoMapper; | ||
| using AspNetCoreHero.Boilerplate.Application.Pipelines; | ||
| using AutoMapper; | ||
| using FluentValidation; | ||
| using MediatR; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
@@ -14,6 +15,8 @@ public static void AddApplicationLayer(this IServiceCollection services) | |
| services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly()); | ||
| services.AddMediatR(Assembly.GetExecutingAssembly()); | ||
| //services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>)); | ||
| services.AddTransient(typeof(IPipelineBehavior<,>), typeof(LoggingBehaviour<,>)); | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not needed line There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Additionaly I'm not sure that is proper place to do it. There could be more suitable place. public static void AddMediatr(this IServiceCollcetion services){
services.AddMediatr(Assembly...
services.AddTransient(typeof(IPipelineBehavior....
}and use it in |
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| using MediatR; | ||
| using Microsoft.Extensions.Logging; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace AspNetCoreHero.Boilerplate.Application.Pipelines | ||
| { | ||
| public class LoggingBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> | ||
| { | ||
| private readonly ILogger<TRequest> _logger; | ||
|
|
||
| public LoggingBehaviour(ILogger<TRequest> logger) | ||
| { | ||
| _logger = logger; | ||
| } | ||
|
|
||
| public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next) | ||
| { | ||
| try | ||
| { | ||
| var requestName = typeof(TRequest).Name; | ||
| _logger.LogInformation("Request: {Name}", requestName); | ||
| return await next(); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| throw ex; | ||
| } | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed