Error with compile-time logging code generation in Microsoft.extension.logging

61 views Asked by At

I have an application with several projects. To refactor the way we handle logging in our application, I created a logging project with a specific provider using the ILoggerProvider interface. I also implemented the ILogger interface.

To add specific logging methods, I used the following implementation:

namespace Logger
{
    public static partial class LogBasicExtensions
    {
        [LoggerMessage(
        EventId = 0,
        Level = LogLevel.Error,
        Message = "Log basic exception")]
        public static partial void LogException(this ILogger logger, Exception exception);

    }
}

Now, to maintain a clean pattern, I wanted each project in the solution to declare the logging methods they would be using. For example, in another project:

namespace AnotherProject
{
    public static partial class ClientLogExtension
    {

        [LoggerMessage(
        EventId = 1,
        Level = LogLevel.Information,
        Message = "Application Started")]
        public static partial void ApplicationStarted(this ILogger logger);

        [LoggerMessage(
        EventId = 2,
        Level = LogLevel.Information,
        Message = "Code {coderead} reception from reader {readernumber}")]
        public static partial void CodeReception(this ILogger logger, string coderead, uint readernumber);
    }
}

However, when trying to compile the project, I encountered the following errors:

CS0757: A partial method may not have multiple implementing declarations. CS0102: The type 'type name' already contains a definition for 'identifier'

Here is the code generated by Microsoft.Extensions.Logging.Generators :

 partial class ClientLogExtension
    {
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "7.0.8.27404")]
        private static readonly global::System.Action<global::Microsoft.Extensions.Logging.ILogger, global::System.Exception?> __ApplicationStartedCallback =
            global::Microsoft.Extensions.Logging.LoggerMessage.Define(global::Microsoft.Extensions.Logging.LogLevel.Information, new global::Microsoft.Extensions.Logging.EventId(1, nameof(ApplicationStarted)), "Application Started", new global::Microsoft.Extensions.Logging.LogDefineOptions() { SkipEnabledCheck = true }); 

        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "7.0.8.27404")]
        public static partial void ApplicationStarted(this global::Microsoft.Extensions.Logging.ILogger logger)
        {
            if (logger.IsEnabled(global::Microsoft.Extensions.Logging.LogLevel.Information))
            {
                __ApplicationStartedCallback(logger, null);
            }
        }
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "7.0.8.27404")]
        private static readonly global::System.Action<global::Microsoft.Extensions.Logging.ILogger, global::System.String, global::System.UInt32, global::System.Exception?> __CodeReceptionCallback =
            global::Microsoft.Extensions.Logging.LoggerMessage.Define<global::System.String, global::System.UInt32>(global::Microsoft.Extensions.Logging.LogLevel.Information, new global::Microsoft.Extensions.Logging.EventId(2, nameof(CodeReception)), "Code {coderead} reception from reader {readernumber}", new global::Microsoft.Extensions.Logging.LogDefineOptions() { SkipEnabledCheck = true }); 

        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "7.0.8.27404")]
        public static partial void CodeReception(this global::Microsoft.Extensions.Logging.ILogger logger, global::System.String coderead, global::System.UInt32 readernumber)
        {
            if (logger.IsEnabled(global::Microsoft.Extensions.Logging.LogLevel.Information))
            {
                __CodeReceptionCallback(logger, coderead, readernumber, null);
            }
        }
    }

So far, I have tried various approaches. In addition to restarting, rebuilding, and deleting the "bin" and "obj" folders, I have attempted to rename my classes and methods. I also created a small test solution that references the Logger project and uses it in the same way, and it worked there.

Now, I know it should work, but it's still not working.

I thought that Visual Studio might be generating two sets of partial classes, so I deleted all the files in the "Temp\VSGeneratedDocuments" directory, but that didn't resolve the issue either.

Any help would be appreciated.

0

There are 0 answers