SerilogAnalyzer
SerilogAnalyzer copied to clipboard
Allow namespaces to be changed to support use cases with wrapper classes
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.
@brianfeucht Trying to understand, How can we now tell the analyser that our new wrapper method exist in a namespace?
@Suchiman Would you please consider merging this if this can make a difference?
@JinsPeter
Here is what we have done (via forked code):
Create an implementation of StructuredLoggerAnalyzer with the following overridden:
ILoggerwith the implementation type you'd like to analyzeLoggerMethodAttributewith 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.