-
-
Notifications
You must be signed in to change notification settings - Fork 150
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 all commits
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,5 +1,5 @@ | ||
| { | ||
| "UseInMemoryDatabase": false, | ||
| "UseInMemoryDatabase": true, | ||
|
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 |
||
| "ConnectionStrings": { | ||
| "ApplicationConnection": "Data Source=Server=(localdb)\\mssqllocaldb;Initial Catalog=AspNetCoreHero.Boilerplate;Integrated Security=True;MultipleActiveResultSets=True", | ||
| "IdentityConnection": "Data Source=(localdb)\\mssqllocaldb;Initial Catalog=AspNetCoreHero.Boilerplate;Integrated Security=True;MultipleActiveResultSets=True" | ||
|
|
||
| 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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| using AspNetCoreHero.Boilerplate.Application.Interfaces.Repositories; | ||
| using AspNetCoreHero.Boilerplate.Application.Exceptions; | ||
|
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 namespace |
||
| using AspNetCoreHero.Boilerplate.Application.Interfaces.Repositories; | ||
| using AspNetCoreHero.Boilerplate.Domain.Entities.Catalog; | ||
| using AspNetCoreHero.Results; | ||
| using AutoMapper; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| using AspNetCoreHero.Boilerplate.Application.Interfaces.Shared; | ||
| 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; | ||
| private readonly IAuthenticatedUserService _authenticatedUserService; | ||
|
|
||
| public LoggingBehaviour( | ||
| ILogger<TRequest> logger, | ||
| IAuthenticatedUserService authenticatedUserService | ||
| ) | ||
| { | ||
| _logger = logger; | ||
| _authenticatedUserService = authenticatedUserService; | ||
| } | ||
|
|
||
| public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next) | ||
| { | ||
| var requestName = typeof(TRequest).Name; | ||
| try | ||
| { | ||
| _logger.LogInformation("{Name} Request From User {UserId}-{UserName}", requestName, _authenticatedUserService.UserId, _authenticatedUserService.Username); | ||
|
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. I would use |
||
| return await next(); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| _logger.LogError("{Name} Request Exception Message : {Error} From User {UserId}-{UserName}", requestName,ex.Message,_authenticatedUserService.UserId,_authenticatedUserService.Username); | ||
| throw; | ||
| } | ||
| } | ||
| } | ||
| } | ||
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 related to the logging behavior. Should be commited in separate PR.