Skip to content

Logging Source Generator fails with a NullReferenceException #52277

Closed

Description

@eerhardt

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

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on May 4, 2021
  2. added
    enhancementProduct code improvement that does NOT require public API changes/additions
    and removed
    untriagedNew issue has not been triaged by the area owner
    on May 4, 2021
  3. added this to the 6.0.0 milestone on May 4, 2021
  4. added and removed
    enhancementProduct code improvement that does NOT require public API changes/additions
    on May 5, 2021
  5. ericstj commented on May 5, 2021

    @ericstj
    Member

    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.

  6. added
    enhancementProduct code improvement that does NOT require public API changes/additions
    on May 5, 2021
  7. ghost added
    in-prThere is an active PR which will close this issue when it is merged
    on May 13, 2021
  8. ghost removed
    in-prThere is an active PR which will close this issue when it is merged
    on Jun 17, 2021
  9. maryamariyan commented on Jun 23, 2021

    @maryamariyan
    Contributor

    Closed via PR #54305

  10. ghost locked as resolved and limited conversation to collaborators on Jul 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-Extensions-LoggingbugenhancementProduct code improvement that does NOT require public API changes/additions

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions