-
Notifications
You must be signed in to change notification settings - Fork 836
Closed
Labels
area-telemetrybugThis issue describes a behavior which is not expected - a bug.This issue describes a behavior which is not expected - a bug.
Description
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 theirToString()
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

Expected behavior:
objectToLog.PropertyToIgnore
wasn't emittedobjectToLog.FieldToLog.Name
andobjectToLog.FieldToLog.Value
were emitted as separate values
Actual behavior

Actual incorrect behavior:
objectToLog.PropertyToIgnore
was emittedToString()
representation ofobjectToLog.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
Assignees
Labels
area-telemetrybugThis issue describes a behavior which is not expected - a bug.This issue describes a behavior which is not expected - a bug.