C
C#12mo ago
M B V R K

Logging code make my code a bit ugly

Hi friends, Hope you're doing well, I'm working on complex project, I use CQRS with MediatR, in the Application layer, also I'm using Serilog ( Console, and File) for logging, and here the issue, the lines of code I wrote to achieve a logging it makes my code a bit a ugly ( in my opinion), because in the Commands and Queries I wrote a logging at the beginning and a logging at the end . The following is a Handle method of a Command :
public override async Task<CreateCategoryCommandResult> Handle(CreateCategoryCommand request, CancellationToken cancellationToken)
{
try
{
Log.Information("CreateCategoryCommandHandler.Handle - Start Creating a new category with name: {Name}, description: {Description}, and user id: {UserId}", request.Name, request.Description, request.UserId);

// Some code goes here

Log.Information("CreateCategoryCommandHandler.Handle - End Creating a new category with name: {Name}, description: {Description}, and user id: {UserId}", request.Name, request.Description, request.UserId);

return result;
}
catch (Exception e)
{
var result = CreateCategoryCommandResult.Failed(e.Message);

Log.Error(e, "CreateCategoryCommandHandler.Handle - Failed to create a new category with name: {Name}, description: {Description}, and user id: {UserId}", request.Name, request.Description, request.UserId);

return result;
}
}
public override async Task<CreateCategoryCommandResult> Handle(CreateCategoryCommand request, CancellationToken cancellationToken)
{
try
{
Log.Information("CreateCategoryCommandHandler.Handle - Start Creating a new category with name: {Name}, description: {Description}, and user id: {UserId}", request.Name, request.Description, request.UserId);

// Some code goes here

Log.Information("CreateCategoryCommandHandler.Handle - End Creating a new category with name: {Name}, description: {Description}, and user id: {UserId}", request.Name, request.Description, request.UserId);

return result;
}
catch (Exception e)
{
var result = CreateCategoryCommandResult.Failed(e.Message);

Log.Error(e, "CreateCategoryCommandHandler.Handle - Failed to create a new category with name: {Name}, description: {Description}, and user id: {UserId}", request.Name, request.Description, request.UserId);

return result;
}
}
Please I want you to share with me your experience about this, <3
10 Replies
Angius
Angius12mo ago
Do you need to log all of that?
Mellow
Mellow12mo ago
I don't know if logging statements inside of a handler is good idea in general, usually I've seen them wrapped around the sender in a custom dispatcher.
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
M B V R K
M B V R KOP12mo ago
lets say yes
Angius
Angius12mo ago
Then I'd at least relegate the exception handling to exception handling middleware Or MediatR pipeline
M B V R K
M B V R KOP12mo ago
hmmmm, seems good idea, but what about other logging lines ???
Mellow
Mellow12mo ago
same thing, they can also be added inside a PipelineBehavior
M B V R K
M B V R KOP12mo ago
great
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server