Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Few logging fixes before the new code generator comes in. #4232

Merged
merged 3 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal static class ModernCodeGen
public static void RefTypes(global::Microsoft.Extensions.Logging.ILogger logger, string connectionId, string type, string streamId, string length, string flags, string other)
{
var state = global::Microsoft.Extensions.Telemetry.Logging.LoggerMessageHelper.ThreadLocalState;
var index = state.EnsurePropertySpace(7);
var index = state.ReservePropertySpace(7);
var array = state.PropertyArray;
array[index++] = new("connectionId", connectionId);
array[index++] = new("type", type);
Expand Down Expand Up @@ -49,7 +49,7 @@ private static string __FMT_0_RefTypes_Error(global::Microsoft.Extensions.Teleme
public static void ValueTypes(global::Microsoft.Extensions.Logging.ILogger logger, long start, long end, int options, global::System.Guid guid)
{
var state = global::Microsoft.Extensions.Telemetry.Logging.LoggerMessageHelper.ThreadLocalState;
var index = state.EnsurePropertySpace(5);
var index = state.ReservePropertySpace(5);
var array = state.PropertyArray;
array[index++] = new("start", start);
array[index++] = new("end", end);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
xakep139 marked this conversation as resolved.
Show resolved Hide resolved
using System.Collections.Generic;
using Microsoft.Extensions.Compliance.Classification;

Check failure on line 6 in src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Logging/LoggerMessageState.EnrichmentPropertyBag.cs

View check run for this annotation

Azure Pipelines / extensions-ci (Correctness WarningsCheck)

src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Logging/LoggerMessageState.EnrichmentPropertyBag.cs#L6

src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Logging/LoggerMessageState.EnrichmentPropertyBag.cs(6,1): error S1128: (NETCORE_ENGINEERING_TELEMETRY=Build) Remove this unnecessary 'using'. (https://rules.sonarsource.com/csharp/RSPEC-1128)

Check failure on line 6 in src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Logging/LoggerMessageState.EnrichmentPropertyBag.cs

View check run for this annotation

Azure Pipelines / extensions-ci (Correctness WarningsCheck)

src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Logging/LoggerMessageState.EnrichmentPropertyBag.cs#L6

src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Logging/LoggerMessageState.EnrichmentPropertyBag.cs(6,1): error S1128: (NETCORE_ENGINEERING_TELEMETRY=Build) Remove this unnecessary 'using'. (https://rules.sonarsource.com/csharp/RSPEC-1128)

Check failure on line 6 in src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Logging/LoggerMessageState.EnrichmentPropertyBag.cs

View check run for this annotation

Azure Pipelines / extensions-ci (Correctness WarningsCheck)

src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Logging/LoggerMessageState.EnrichmentPropertyBag.cs#L6

src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Logging/LoggerMessageState.EnrichmentPropertyBag.cs(6,1): error S1128: (NETCORE_ENGINEERING_TELEMETRY=Build) Remove this unnecessary 'using'. (https://rules.sonarsource.com/csharp/RSPEC-1128)
using Microsoft.Extensions.Telemetry.Enrichment;

namespace Microsoft.Extensions.Telemetry.Logging;

public partial class LoggerMessageState : IEnrichmentPropertyBag
{
/// <inheritdoc/>
void IEnrichmentPropertyBag.Add(string key, object value)
{
AddProperty(key, value);
}

/// <inheritdoc/>
void IEnrichmentPropertyBag.Add(string key, string value)
{
AddProperty(key, value);
}

/// <inheritdoc/>
void IEnrichmentPropertyBag.Add(ReadOnlySpan<KeyValuePair<string, object>> properties)
{
foreach (var p in properties)
{
AddProperty(p.Key, p.Value);
geeknoid marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// <inheritdoc/>
void IEnrichmentPropertyBag.Add(ReadOnlySpan<KeyValuePair<string, string>> properties)
{
foreach (var p in properties)
{
AddProperty(p.Key, p.Value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@ public partial class LoggerMessageState : ILogPropertyCollector
/// <inheritdoc />
void ILogPropertyCollector.Add(string propertyName, object? propertyValue)
{
string fullName = PropertyNamePrefix.Length > 0 ? PropertyNamePrefix + propertyName : propertyName;
var index = EnsurePropertySpace(1);
_properties[index] = new(fullName, propertyValue);
string fullName = PropertyNamePrefix.Length > 0 ? PropertyNamePrefix + "_" + propertyName : propertyName;
AddProperty(fullName, propertyValue);
}

/// <inheritdoc />
void ILogPropertyCollector.Add(string propertyName, object? propertyValue, DataClassification classification)
{
string fullName = PropertyNamePrefix.Length > 0 ? PropertyNamePrefix + propertyName : propertyName;
var index = EnsureClassifiedPropertySpace(1);
_classifiedProperties[index] = new(fullName, propertyValue, classification);
string fullName = PropertyNamePrefix.Length > 0 ? PropertyNamePrefix + "_" + propertyName : propertyName;
geeknoid marked this conversation as resolved.
Show resolved Hide resolved
AddClassifiedProperty(fullName, propertyValue, classification);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.Compliance.Classification;
using Microsoft.Extensions.Logging;
using Microsoft.Shared.DiagnosticIds;
using Microsoft.Shared.Pools;
Expand Down Expand Up @@ -44,7 +45,7 @@ public sealed partial class LoggerMessageState
/// </summary>
/// <param name="count">The amount of space to allocate.</param>
/// <returns>The index in the <see cref="PropertyArray"/> where to store the properties.</returns>
public int EnsurePropertySpace(int count)
public int ReservePropertySpace(int count)
{
int avail = _properties.Length - NumProperties;
if (count > avail)
Expand All @@ -63,7 +64,7 @@ public int EnsurePropertySpace(int count)
/// </summary>
/// <param name="count">The amount of space to allocate.</param>
/// <returns>The index in the <see cref="RedactedPropertyArray"/> where to store the properties.</returns>
public int EnsureRedactedPropertySpace(int count)
public int ReserveRedactedPropertySpace(int count)
{
int avail = _redactedProperties.Length - NumRedactedProperties;
if (count > avail)
Expand All @@ -82,7 +83,7 @@ public int EnsureRedactedPropertySpace(int count)
/// </summary>
/// <param name="count">The amount of space to allocate.</param>
/// <returns>The index in the <see cref="ClassifiedPropertyArray"/> where to store the classified properties.</returns>
public int EnsureClassifiedPropertySpace(int count)
public int ReserveClassifiedPropertySpace(int count)
{
int avail = _classifiedProperties.Length - NumClassifiedProperties;
if (count > avail)
Expand All @@ -96,6 +97,29 @@ public int EnsureClassifiedPropertySpace(int count)
return index;
}

/// <summary>
/// Adds a property to the array.
/// </summary>
/// <param name="name">The name of the property.</param>
/// <param name="value">The value.</param>
public void AddProperty(string name, object? value)
{
var index = ReservePropertySpace(1);
PropertyArray[index] = new(name, value);
}

/// <summary>
/// Adds a classified property to the array.
/// </summary>
/// <param name="name">The name of the property.</param>
/// <param name="value">The value.</param>
/// <param name="classification">The data classification of the property.</param>
public void AddClassifiedProperty(string name, object? value, DataClassification classification)
{
var index = ReserveClassifiedPropertySpace(1);
ClassifiedPropertyArray[index] = new(name, value, classification);
}

/// <summary>
/// Resets state of this object to its initial condition.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,22 @@ private void ModernPath(LogLevel logLevel, EventId eventId, LoggerMessageState m
try
{
ref var cp = ref msgState.ClassifiedPropertyArray[i];
var jr = JustInTimeRedactor.Get();
jr.Value = cp.Value;
jr.Redactor = config.GetRedactor(cp.Classification);
jr.Next = jitRedactors;
jitRedactors = jr;

var index = msgState.EnsureRedactedPropertySpace(1);
msgState.RedactedPropertyArray[index] = new(cp.Name, jr);
if (cp.Value != null)
{
var jr = JustInTimeRedactor.Get();
jr.Value = cp.Value;
jr.Redactor = config.GetRedactor(cp.Classification);
jr.Next = jitRedactors;
jitRedactors = jr;

var index = msgState.ReserveRedactedPropertySpace(1);
msgState.RedactedPropertyArray[index] = new(cp.Name, jr);
}
else
{
var index = msgState.ReserveRedactedPropertySpace(1);
msgState.RedactedPropertyArray[index] = new(cp.Name, null);
}
}
catch (Exception ex)
{
Expand All @@ -218,6 +226,7 @@ private void ModernPath(LogLevel logLevel, EventId eventId, LoggerMessageState m
var joiner = ModernJoiner;
joiner.StaticProperties = config.StaticProperties;
joiner.Formatter = formatter;
joiner.State = msgState;
joiner.SetIncomingProperties(msgState);

// enrich
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace Microsoft.Extensions.Telemetry.Logging;
internal sealed class JustInTimeRedactor : IResettable
#if NET6_0_OR_GREATER
, ISpanFormattable
#else
, IFormattable
#endif
{
public static JustInTimeRedactor Get() => _pool.Get();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// 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.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Compliance.Testing;
using Microsoft.Extensions.Telemetry.Enrichment;
using Xunit;

namespace Microsoft.Extensions.Telemetry.Logging.Test;
Expand All @@ -19,7 +21,7 @@ public static void Basic()

var lms = new LoggerMessageState();

var index = lms.EnsurePropertySpace(1);
var index = lms.ReservePropertySpace(1);
lms.PropertyArray[index] = new(PropName, Value);
Assert.Equal(1, lms.NumProperties);
Assert.Equal(PropName, lms.PropertyArray[0].Key);
Expand All @@ -30,14 +32,14 @@ public static void Basic()
Assert.Equal(0, lms.NumProperties);
Assert.Equal("", lms.ToString());

index = lms.EnsurePropertySpace(1);
index = lms.ReservePropertySpace(1);
lms.PropertyArray[index] = new(PropName, Value);
Assert.Equal(1, lms.NumProperties);
Assert.Equal(PropName, lms.PropertyArray[0].Key);
Assert.Equal(Value, lms.PropertyArray[0].Value);
Assert.Equal("Property Name=Value", lms.ToString());

index = lms.EnsureClassifiedPropertySpace(1);
index = lms.ReserveClassifiedPropertySpace(1);
lms.ClassifiedPropertyArray[index] = new(PropName, Value, SimpleClassifications.PrivateData);
Assert.Equal(1, lms.NumProperties);
Assert.Equal(PropName, lms.PropertyArray[0].Key);
Expand All @@ -50,7 +52,7 @@ public static void Basic()
Assert.Equal(SimpleClassifications.PrivateData, lms.ClassifiedPropertyArray[0].Classification);
Assert.Equal("Property Name=Value,Property Name=Microsoft.Extensions.Compliance.Testing.SimpleTaxonomy:2", lms.ToString());

index = lms.EnsurePropertySpace(1);
index = lms.ReservePropertySpace(1);
lms.PropertyArray[index] = new(PropName + "X", Value);
Assert.Equal(2, lms.NumProperties);
Assert.Equal(2, lms.PropertyArray.Length);
Expand All @@ -61,10 +63,37 @@ public static void Basic()
Assert.Equal("Property Name=Value,Property NameX=Value,Property Name=Microsoft.Extensions.Compliance.Testing.SimpleTaxonomy:2", lms.ToString());
}

[Fact]
public static void PropertyBagContract()
{
var lms = new LoggerMessageState();

var bag = (IEnrichmentPropertyBag)lms;

bag.Add("K1", "V1");
bag.Add("K2", (object)"V2");
bag.Add(new[] { new KeyValuePair<string, string>("K3", "V3") }.AsSpan());
bag.Add(new[] { new KeyValuePair<string, object>("K4", "V4") }.AsSpan());

Assert.Equal(4, lms.NumProperties);
Assert.Equal(0, lms.NumClassifiedProperties);
Assert.Equal(0, lms.NumRedactedProperties);

Assert.Equal("K1", lms.PropertyArray[0].Key);
Assert.Equal("K2", lms.PropertyArray[1].Key);
Assert.Equal("K3", lms.PropertyArray[2].Key);
Assert.Equal("K4", lms.PropertyArray[3].Key);

Assert.Equal("V1", lms.PropertyArray[0].Value);
Assert.Equal("V2", lms.PropertyArray[1].Value);
Assert.Equal("V3", lms.PropertyArray[2].Value);
Assert.Equal("V4", lms.PropertyArray[3].Value);
}

[Fact]
public static void CollectorContract()
{
const string PropertyNamPrefix = "param_name_";
const string PropertyNamPrefix = "param_name";
const string PropName = "Property Name";
const string Value = "Value";

Expand All @@ -75,16 +104,16 @@ public static void CollectorContract()

collector.Add(PropName, Value);
Assert.Equal(1, lms.NumProperties);
Assert.Equal(PropertyNamPrefix + PropName, lms.PropertyArray[0].Key);
Assert.Equal(PropertyNamPrefix + "_" + PropName, lms.PropertyArray[0].Key);
Assert.Equal(Value, lms.PropertyArray[0].Value);

collector.Add(PropName, Value, SimpleClassifications.PrivateData);
Assert.Equal(1, lms.NumProperties);
Assert.Equal(PropertyNamPrefix + PropName, lms.PropertyArray[0].Key);
Assert.Equal(PropertyNamPrefix + "_" + PropName, lms.PropertyArray[0].Key);
Assert.Equal(Value, lms.PropertyArray[0].Value);

Assert.Equal(1, lms.NumClassifiedProperties);
Assert.Equal(PropertyNamPrefix + PropName, lms.ClassifiedPropertyArray[0].Name);
Assert.Equal(PropertyNamPrefix + "_" + PropName, lms.ClassifiedPropertyArray[0].Name);
Assert.Equal(Value, lms.ClassifiedPropertyArray[0].Value);
Assert.Equal(SimpleClassifications.PrivateData, lms.ClassifiedPropertyArray[0].Classification);

Expand Down Expand Up @@ -116,7 +145,7 @@ public static void ReadOnlyListContract()
var lms = new LoggerMessageState();
var list = (IReadOnlyList<KeyValuePair<string, object?>>)lms;

var index = lms.EnsurePropertySpace(1);
var index = lms.ReservePropertySpace(1);
lms.PropertyArray[index] = new(PropName, Value);
Assert.Equal(1, list.Count);
Assert.Equal(PropName, list[0].Key);
Expand All @@ -126,13 +155,13 @@ public static void ReadOnlyListContract()
lms.Clear();
Assert.Empty(list);

index = lms.EnsurePropertySpace(1);
index = lms.ReservePropertySpace(1);
lms.PropertyArray[index] = new(PropName, Value);
Assert.Equal(1, list.Count);
Assert.Equal(PropName, list[0].Key);
Assert.Equal(Value, list[0].Value);

index = lms.EnsureClassifiedPropertySpace(1);
index = lms.ReserveClassifiedPropertySpace(1);
lms.ClassifiedPropertyArray[index] = new(PropName, Value, SimpleClassifications.PrivateData);
Assert.Equal(1, list.Count);
Assert.Equal(PropName, list[0].Key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ public static void Basic()
logger.Log(LogLevel.Error, new EventId(1, "ID1"), lmh, null, (_, _) => "MSG1");

var lms = LoggerMessageHelper.ThreadLocalState;
var index = lms.EnsurePropertySpace(1);
var index = lms.ReservePropertySpace(1);
lms.PropertyArray[index] = new("PK2", "PV2");

index = lms.EnsureClassifiedPropertySpace(1);
index = lms.ReserveClassifiedPropertySpace(2);
lms.ClassifiedPropertyArray[index] = new("PK3", "PV3", SimpleClassifications.PrivateData);
lms.ClassifiedPropertyArray[index + 1] = new("PK4", null, SimpleClassifications.PrivateData);

logger.Log(LogLevel.Warning, new EventId(2, "ID2"), lms, null, (_, _) => "MSG2");

Expand Down Expand Up @@ -95,6 +96,7 @@ public static void Basic()
Assert.Equal("MSG2", snap[2].Message);
Assert.Equal("PV2", snap[2].StructuredState!.GetValue("PK2"));
Assert.Equal("REDACTED<PV3>", snap[2].StructuredState!.GetValue("PK3"));
Assert.Null(snap[2].StructuredState!.GetValue("PK4"));
Assert.Equal("EV1", snap[2].StructuredState!.GetValue("EK1"));
Assert.Equal("SEV1", snap[2].StructuredState!.GetValue("SEK1"));
}
Expand Down Expand Up @@ -132,7 +134,7 @@ public static void BagAndJoiner(bool objectVersion)
logger.Log(LogLevel.Error, new EventId(1, "ID1"), lmh, null, (_, _) => "MSG1");

var lms = LoggerMessageHelper.ThreadLocalState;
var index = lms.EnsurePropertySpace(1);
var index = lms.ReservePropertySpace(1);
lms.PropertyArray[index] = new("PK2", "PV2");
logger.Log(LogLevel.Warning, new EventId(2, "ID2"), lms, null, (_, _) => "MSG2");

Expand Down