Skip to content

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

@matteotrapani

Description

@matteotrapani

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

// 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;

/// <summary>
/// Marks a logging method parameter whose public properties need to be logged as log tags.
/// </summary>
/// <seealso cref="LoggerMessageAttribute"/>
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property)]
public sealed class LogPropertiesAttribute : Attribute
{
    /// <summary>
    /// Gets or sets a value indicating whether <see langword="null"/> properties are logged.
    /// </summary>
    /// <value>
    /// Defaults to <see langword="false"/>.
    /// </value>
    public bool SkipNullProperties { get; set; }

    /// <summary>
    /// 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.
    /// </summary>
    /// <value>
    /// Defaults to <see langword="false"/>.
    /// </value>
    public bool OmitReferenceName { get; set; }

    /// <summary>
    /// Gets or sets a value indicating whether to transitively visit properties which are complex objects.
    /// </summary>
    /// <remarks>
    /// When logging the properties of an object, this property controls the behavior for each encountered property.
    /// When this property is <see langword="false"/>, then each property is serialized by calling <see cref="object.ToString" /> to
    /// generate a string for the property. When this property is <see langword="true"/>, then each property of any complex objects are
    /// expanded individually.
    /// </remarks>
    /// <value>
    /// Defaults to <see langword="false"/>.
    /// </value>
    public bool Transitive { get; set; }
}

API Usage

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

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions