Merge branch 'newmain'

This commit is contained in:
masoodafar-web
2025-11-13 23:27:53 +03:30
parent c028809d08
commit c48a04d5f9
13 changed files with 280 additions and 23 deletions

View File

@@ -0,0 +1,38 @@
using Grpc.Core.Interceptors;
using Microsoft.Extensions.Logging;
using FrontOffice.BFF.Application.Common.Interfaces;
namespace FrontOffice.BFF.WebApi.Common.Behaviours;
public class LoggingBehaviour : Interceptor
{
private readonly ILogger _logger;
private readonly ICurrentUserService _currentUserService;
public LoggingBehaviour(ILogger<LoggingBehaviour> logger, ICurrentUserService currentUserService)
{
_logger = logger;
_currentUserService = currentUserService;
}
public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(
TRequest request,
ServerCallContext context,
UnaryServerMethod<TRequest, TResponse> continuation)
{
var requestName = typeof(TRequest).Name;
var userId = _currentUserService.UserId ?? string.Empty;
_logger.LogInformation("gRPC Starting receiving call. Type/Method: {Type} / {Method} Request: {Name} {@UserId} {@Request}",
MethodType.Unary, context.Method , requestName, userId, request);
try
{
return await continuation(request, context);
}
catch (Exception ex)
{
_logger.LogError(ex, "gRPC Request: Unhandled Exception for Request {Name} {@Request}", requestName, request);
throw;
}
}
}

View File

@@ -0,0 +1,44 @@
using Grpc.Core.Interceptors;
using Microsoft.Extensions.Logging;
using System.Diagnostics;
using FrontOffice.BFF.Application.Common.Interfaces;
namespace FrontOffice.BFF.WebApi.Common.Behaviours;
public class PerformanceBehaviour : Interceptor
{
private readonly Stopwatch _timer;
private readonly ILogger _logger;
private readonly ICurrentUserService _currentUserService;
public PerformanceBehaviour(ILogger<PerformanceBehaviour> logger, ICurrentUserService currentUserService)
{
_timer = new Stopwatch();
_logger = logger;
_currentUserService = currentUserService;
}
public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(
TRequest request,
ServerCallContext context,
UnaryServerMethod<TRequest, TResponse> continuation)
{
_timer.Start();
var response = await continuation(request, context);
_timer.Stop();
var elapsedMilliseconds = _timer.ElapsedMilliseconds;
if (elapsedMilliseconds > 500)
{
var requestName = typeof(TRequest).Name;
var userId = _currentUserService.UserId ?? string.Empty;
_logger.LogWarning("gRPC Long Running Request: {Name} ({ElapsedMilliseconds} milliseconds) {@UserId} {@Request}",
requestName, elapsedMilliseconds, userId, request);
}
return response;
}
}

View File

@@ -1,4 +1,4 @@
using FrontOffice.BFF.User.Protobuf.Protos.User;
using FrontOffice.BFF.Protobuf.Protos.User;
using FrontOffice.BFF.WebApi.Common.Services;
using FrontOffice.BFF.Application.UserCQ.Commands.UpdateUser;
using FrontOffice.BFF.Application.UserCQ.Commands.DeleteUser;
@@ -6,6 +6,8 @@ using FrontOffice.BFF.Application.UserCQ.Queries.GetUser;
using FrontOffice.BFF.Application.UserCQ.Queries.GetAllUserByFilter;
using FrontOffice.BFF.Application.UserCQ.Commands.CreateNewOtpToken;
using FrontOffice.BFF.Application.UserCQ.Commands.VerifyOtpToken;
using FrontOffice.BFF.Application.UserCQ.Queries.AdminGetJwtToken;
using FrontOffice.BFF.Application.UserCQ.Commands.SetPasswordForUser;
namespace FrontOffice.BFF.WebApi.Services;
public class UserService : UserContract.UserContractBase
{
@@ -39,4 +41,12 @@ public class UserService : UserContract.UserContractBase
{
return await _dispatchRequestToCQRS.Handle<VerifyOtpTokenRequest, VerifyOtpTokenCommand, VerifyOtpTokenResponse>(request, context);
}
public override async Task<AdminGetJwtTokenResponse> AdminGetJwtToken(AdminGetJwtTokenRequest request, ServerCallContext context)
{
return await _dispatchRequestToCQRS.Handle<AdminGetJwtTokenRequest, AdminGetJwtTokenQuery, AdminGetJwtTokenResponse>(request, context);
}
public override async Task<Empty> SetPasswordForUser(SetPasswordForUserRequest request, ServerCallContext context)
{
return await _dispatchRequestToCQRS.Handle<SetPasswordForUserRequest, SetPasswordForUserCommand>(request, context);
}
}