© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•16mo ago
velreine

ILogger<T> extension method "pass the args".

I am trying to create an extension method on ILogger<T> called
LogPermissionDenied
LogPermissionDenied
.

I have a custom
IPermissionDeniedErrorFragment
IPermissionDeniedErrorFragment
which is an auto-generated interface from Strawberry shake based on a GraphQL fragment, that doesn't really matter that much, it is just to provide some context.

The
ILogger<T>
ILogger<T>
has a method called
LogError
LogError
with an overload that accepts an
string message
string message
and a
params object?[] args
params object?[] args


I want to be able to call the
LogPermissionDenied
LogPermissionDenied
function and call it with a string that is contextual to the location it occurs in the code, the extension method should then append some more information regarding the permission that was denied.

Here is the implementation of the extension method itself (I have attempted a few things but this is what it looks like so far.)

    public static void LogPermissionDenied<T>(
        this ILogger<T> logger,
        [StructuredMessageTemplate] string message,
        IPermissionFragment permission,
        params object?[] args)
    {
        //object?[] newArgs = args.Append(new [])

        var messageFormat = "Hello {name}";
        
        var formattedMessage = string.Format(message, args);
        
        
        logger.LogWarning("""
            {errorMessage}
            (Permission):
            Id: {permissionId}
            Name: {permissionName}
            Description: {permissionDescription}              
            """, formattedMessage, permission.Id, permission.Name, permission.Description);
    }
    public static void LogPermissionDenied<T>(
        this ILogger<T> logger,
        [StructuredMessageTemplate] string message,
        IPermissionFragment permission,
        params object?[] args)
    {
        //object?[] newArgs = args.Append(new [])

        var messageFormat = "Hello {name}";
        
        var formattedMessage = string.Format(message, args);
        
        
        logger.LogWarning("""
            {errorMessage}
            (Permission):
            Id: {permissionId}
            Name: {permissionName}
            Description: {permissionDescription}              
            """, formattedMessage, permission.Id, permission.Name, permission.Description);
    }


This is how i want to use that extension method:

this._logger.LogPermissionDenied("""
                Doing business logic related thing for entity with id: {entityId} failed due to insufficient permissions.
                """, permissionDeniedError.Permission!, entityId);
this._logger.LogPermissionDenied("""
                Doing business logic related thing for entity with id: {entityId} failed due to insufficient permissions.
                """, permissionDeniedError.Permission!, entityId);


Its important the params will appear in
customDimensions
customDimensions
in Application Insights, and that it is not possible to "screw up" the params (since they are positional).

Any ideas?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

ILogger<T>
C#CC# / help
2y ago
❔ use of args in method
C#CC# / help
3y ago
ILogger question.
C#CC# / help
13mo ago