Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow namespaces to be changed to support use cases with wrapper classes #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

brianfeucht
Copy link

We use wrappers around Serilog in our implementation. This means this analyzer didn't work for us since it was looking for specific namespaces. By allowing these namespaces to be updated we can utilize this analyzer without any additional code changes.

Copy link

@NielsPilgaard NielsPilgaard left a comment

Choose a reason for hiding this comment

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

Nice update, fingers crossed it'll be merged eventually 🤞

@JinsPeter
Copy link

@brianfeucht Trying to understand,
How can we now tell the analyser that our new wrapper method exist in a namespace?

@JinsPeter
Copy link

@Suchiman Would you please consider merging this if this can make a difference?

@brianfeucht
Copy link
Author

@JinsPeter

Here is what we have done (via forked code):

Create an implementation of StructuredLoggerAnalyzer with the following overridden:

  • ILogger with the implementation type you'd like to analyze
  • LoggerMethodAttribute with a new attribute type

We then decorate our implementation type with the new attribute.

    [AttributeUsage(AttributeTargets.Constructor | AttributeTargets.Method)]
    public sealed class MessageTemplateFormatMethodAttribute : Attribute
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MessageTemplateFormatMethodAttribute"/> class.
        /// </summary>
        /// <param name="messageTemplateParameterName">Name of the message template parameter.</param>
        public MessageTemplateFormatMethodAttribute(string messageTemplateParameterName)
        {
            MessageTemplateParameterName = messageTemplateParameterName;
        }

        /// <summary>
        /// Gets the name of the message template parameter.
        /// </summary>
        /// <value>The name of the message template parameter.</value>
        public string MessageTemplateParameterName { get; private set; }
    }
    
    public interface IStructuredLogger 
    { 
        [MessageTemplateFormatMethod("messageTemplate")] void Debug(Exception exception, string messageTemplate, params object[] propertyValues);
        [MessageTemplateFormatMethod("messageTemplate")] void Debug(string messageTemplate, params object[] propertyValues);
    }
    
    public StructuredLoggerAnalyzer : DiagnosticAnalyzer {
        protected virtual string ILogger => "IStructuredLogger ";
        protected virtual string LoggerMethodAttribute => "MessageTemplateFormatMethodAttribute";
    }

When then build and package up this project as our own analyzer and import it into our project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants