© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
17 replies
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
CQRS
with
MediatR
MediatR
, in the
Application
Application
layer, also I'm using
Serilog
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
Commands
and
Queries
Queries
I wrote a logging at the beginning and a logging at the end .

The following is a
Handle
Handle
method of a
Command
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
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Code stops after a bit
C#CC# / help
4y ago
✅ How can I make my code reusable?
C#CC# / help
3y ago
In my code, it says I have a null parameter, this is my first bit of code, Where am I going wrong?
C#CC# / help
3y ago