dotnet/runtime

Logging Source Generator fails with a NullReferenceException

eerhardt opened this issue · 3 comments

Repro

  1. Create a new ClassLibrary
  2. Add <PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0-preview.4.21222.10" />
  3. Write the following code in Class1.cs
using System;
using Microsoft.Extensions.Logging;

namespace ClassLibrary1
{
    public partial class Class1
    {
        private ILogger _logger;
        public Class1(ILogger logger) => _logger = logger;

        internal static class EventIds
        {
            public const int HealthCheckErrorId = 100;
        }

        [LoggerMessage(EventId = HealthCheckErrorId, Level = LogLevel.Error, Message = "Health check {HealthCheckName} threw an unhandled exception after {ElapsedMilliseconds}ms")]
        private partial void HealthCheckError(string HealthCheckName, double ElapsedMilliseconds, Exception exception);
    }
}
  1. Build

Actual results

I'm seeing the following warning in my Error List

Severity	Code	Description	Project	File	Line	Suppression State
Warning	CS8785	Generator 'LoggerMessageGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'NullReferenceException' with message 'Object reference not set to an instance of an object.'	ClassLibrary1	C:\Users\eerhardt\source\repos\ClassLibrary1\ClassLibrary1\CSC	1	Active

Expected results

I only expect my compile error (here I didn't put EventIds. in front of HealthCheckErrorId in the LoggerMessage attribute) to exist in the Error List. I don't expect the Logging Source Generator to fail and add more errors.

cc @maryamariyan

Tagging subscribers to this area: @maryamariyan
See info in area-owners.md if you want to be subscribed.

Issue Details

Repro

  1. Create a new ClassLibrary
  2. Add <PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0-preview.4.21222.10" />
  3. Write the following code in Class1.cs
using System;
using Microsoft.Extensions.Logging;

namespace ClassLibrary1
{
    public partial class Class1
    {
        private ILogger _logger;
        public Class1(ILogger logger) => _logger = logger;

        internal static class EventIds
        {
            public const int HealthCheckErrorId = 100;
        }

        [LoggerMessage(EventId = HealthCheckErrorId, Level = LogLevel.Error, Message = "Health check {HealthCheckName} threw an unhandled exception after {ElapsedMilliseconds}ms")]
        private partial void HealthCheckError(string HealthCheckName, double ElapsedMilliseconds, Exception exception);
    }
}
  1. Build

Actual results

I'm seeing the following warning in my Error List

Severity	Code	Description	Project	File	Line	Suppression State
Warning	CS8785	Generator 'LoggerMessageGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'NullReferenceException' with message 'Object reference not set to an instance of an object.'	ClassLibrary1	C:\Users\eerhardt\source\repos\ClassLibrary1\ClassLibrary1\CSC	1	Active

Expected results

I only expect my compile error (here I didn't put EventIds. in front of HealthCheckErrorId in the LoggerMessage attribute) to exist in the Error List. I don't expect the Logging Source Generator to fail and add more errors.

cc @maryamariyan

Author: eerhardt
Assignees: -
Labels:

area-Extensions-Logging, untriaged

Milestone: -

I only expect my compile error (here I didn't put EventIds. in front of HealthCheckErrorId in the LoggerMessage attribute) to exist in the Error List. I don't expect the Logging Source Generator to fail and add more errors.

Agree with @eerhardt that we need to ensure our source generators are resilient to bad syntax in user's source files (cc @layomia in case similar concerns apply to Json). We probably need test cases for invalid source to ensure our generators handle these cases well.

Closed via PR #54305