Skip to content

[Logging] LogPropertyIgnore and LogProperties attributes don't work if an object being logged resides in a different assembly than the logging method #6598

@iliar-turdushev

Description

@iliar-turdushev

Description

If an object being logged resides in one assembly and the logging method in another then:

  • its properties marked as LogPropertyIgnore are still being logged, although they must not be emitted
  • for properties marked as LogProperties only their ToString() representation is logged, although each individual property must be logged separately

Reproduction Steps

A sample application consists of two projects. ProjectA is a class library that includes an object to be logged, and ProjectB is a console application that logs the object defined in ProjectA.

ProjectA

ProjectA.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Telemetry.Abstractions" Version="9.6.0" />
  </ItemGroup>
</Project>

ObjectToLog.cs

using Microsoft.Extensions.Logging;

namespace ProjectA;

public class ObjectToLog
{
    // This property MUST NOT be logged.
    [LogPropertyIgnore]
    public string? PropertyToIgnore { get; set; }

    // This property MUST be logged.
    public string? PropertyToLog { get; set; }

    // This property MUST be logged. Each property of FieldToLog MUST be logged separately.
    [LogProperties]
    public Field? FieldToLog { get; set; }
}

public class Field
{
    public string? Name { get; set; }
    public string? Value { get; set; }
}

ProjectB

ProjectB.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Telemetry.Abstractions" Version="9.6.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.5" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\ProjectA\ProjectA.csproj" />
  </ItemGroup>
</Project>

Log.cs

using Microsoft.Extensions.Logging;
using ProjectA;

internal static partial class Log
{
    [LoggerMessage(LogLevel.Information)]
    public static partial void LogObject(this ILogger logger, [LogProperties] ObjectToLog objectToLog);
}

Program.cs

using Microsoft.Extensions.Logging;
using ProjectA;

using ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
{
    builder.ClearProviders();
    builder.AddJsonConsole(options => options.JsonWriterOptions = new() { Indented = true });
});

ILogger logger = loggerFactory.CreateLogger("Demo");

logger.LogObject(new ObjectToLog
{
    PropertyToIgnore = "Foo",
    PropertyToLog = "Bar",
    FieldToLog = new Field { Name = "Fizz", Value = "Buzz" }
});

Expected behavior

Image

Expected behavior:

  • objectToLog.PropertyToIgnore wasn't emitted
  • objectToLog.FieldToLog.Name and objectToLog.FieldToLog.Value were emitted as separate values

Actual behavior

Image

Actual incorrect behavior:

  • objectToLog.PropertyToIgnore was emitted
  • ToString() representation of objectToLog.FieldToLog was emitted instead of its individual properties

Regression?

No response

Known Workarounds

Put the following line at the beginning of the file that contains a class to log whose properties are marked as LogPropertyIgnore or LogProperties:

#define CODE_GENERATION_ATTRIBUTES

public class ObjectToLog
{
    ...
}

Configuration

No response

Other information

No response

Metadata

Metadata

Labels

area-telemetrybugThis issue describes a behavior which is not expected - a bug.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions