Skip to content
Open
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
@@ -1,5 +1,6 @@
using AspNetCoreHero.Boilerplate.Application.Interfaces.Shared;
using Microsoft.AspNetCore.Http;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace AspNetCoreHero.Boilerplate.Api.Services
Expand All @@ -9,6 +10,7 @@ public class AuthenticatedUserService : IAuthenticatedUserService
public AuthenticatedUserService(IHttpContextAccessor httpContextAccessor)
{
UserId = httpContextAccessor.HttpContext?.User?.FindFirstValue("uid");
Username = httpContextAccessor.HttpContext?.User?.FindFirstValue(ClaimTypes.NameIdentifier);

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.

}

public string UserId { get; }
Expand Down
2 changes: 1 addition & 1 deletion AspNetCoreHero.Boilerplate.Api/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"UseInMemoryDatabase": false,
"UseInMemoryDatabase": true,

Choose a reason for hiding this comment

The 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"
Expand Down
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;
Expand All @@ -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<,>));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed line

Choose a reason for hiding this comment

The 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.
For an example create method like below:

public static void AddMediatr(this IServiceCollcetion services){
   services.AddMediatr(Assembly...
   services.AddTransient(typeof(IPipelineBehavior....
}

and use it in AddApplicationLayer method instead of line 16. (services.AddMediatr...)

}
}
}
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;

Choose a reason for hiding this comment

The 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;
Expand Down
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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use _logger.LogDebug( because it can be very big amount of data.

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;
}
}
}
}