dotnet / dotnet/extensions

[API Proposal]: LogProperties attribute: serialize nested objects in collections instead of ToString

Open
#6,795 0 comments 0 reactions 0 assignees View on GitHub
api-suggestion area-telemetry untriaged
Dominant language
C#
Stars
3.2k
Forks
894
Avg merge
1d 12h
Merged PRs (30d)
23

Description

### Background and motivation

Related issue #6785

Hello,

The `[LogProperties]` attribute is great, but it's limited when it comes to logging complex objects.
The `Transitive` property works fine, but raises the warning `EXTEXP003` .

The `Experimental` attribute should be removed from that property.

### API Proposal

```csharp
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.Logging;
using Microsoft.Shared.DiagnosticIds;

namespace Microsoft.Extensions.Logging;

///
/// Marks a logging method parameter whose public properties need to be logged as log tags.
///
///
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property)]
public sealed class LogPropertiesAttribute : Attribute
{
///
/// Gets or sets a value indicating whether properties are logged.
///
///
/// Defaults to .
///
public bool SkipNullProperties { get; set; }

///
/// Gets or sets a value indicating whether to prefix the name of the parameter or property to the generated name of each tag being logged.
///
///
/// Defaults to .
///
public bool OmitReferenceName { get; set; }

///
/// Gets or sets a value indicating whether to transitively visit properties which are complex objects.
///
///
/// When logging the properties of an object, this property controls the behavior for each encountered property.
/// When this property is , then each property is serialized by calling to
/// generate a string for the property. When this property is , then each property of any complex objects are
/// expanded individually.
///
///
/// Defaults to .
///
public bool Transitive { get; set; }
}
```

### API Usage

```csharp
[LoggerMessage(LogLevel.Debug, "Request debug. Uri: {Uri}")]
partial void LogRequest(string uri, [LogProperties(Transitive = true)] Request payload);
```

### Alternative Designs

_No response_

### Risks

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.