Skip to content

Commit 8a91c15

Browse files
Move shared files to Shared project and add more tests
1 parent a79fcbf commit 8a91c15

14 files changed

+55
-205
lines changed

eng/MSBuild/Shared.props

+4
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,8 @@
4646
<ItemGroup Condition="'$(InjectStringSplitExtensions)' == 'true'">
4747
<Compile Include="$(MSBuildThisFileDirectory)\..\..\src\Shared\StringSplit\*.cs" LinkBase="Shared\StringSplit" />
4848
</ItemGroup>
49+
50+
<ItemGroup Condition="'$(InjectSharedLoggingBuffering)' == 'true'">
51+
<Compile Include="$(MSBuildThisFileDirectory)\..\..\src\Shared\LoggingBuffering\*.cs" LinkBase="Shared\LoggingBuffering" />
52+
</ItemGroup>
4953
</Project>

src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Buffering/DeserializedLogRecord.cs

-64
This file was deleted.

src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Buffering/HttpRequestBufferHolder.cs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Concurrent;
6+
using Microsoft.Extensions.Diagnostics.Buffering;
67

78
namespace Microsoft.AspNetCore.Diagnostics.Buffering;
89

src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Buffering/ILoggingBuffer.cs

-37
This file was deleted.

src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Buffering/SerializedLogRecord.cs

-90
This file was deleted.

src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Microsoft.AspNetCore.Diagnostics.Middleware.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
<ForceLatestDotnetVersions>true</ForceLatestDotnetVersions>
1212
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
1313
<UseLoggingGenerator>true</UseLoggingGenerator>
14-
<InjectSharedThrow>false</InjectSharedThrow>
15-
<InjectTrimAttributesOnLegacy>false</InjectTrimAttributesOnLegacy>
14+
<InjectExperimentalAttributeOnLegacy>true</InjectExperimentalAttributeOnLegacy>
1615
<InjectSharedDebugger>true</InjectSharedDebugger>
1716
<InjectSharedDataValidation>false</InjectSharedDataValidation>
1817
<InjectSharedPools>false</InjectSharedPools>
1918
<InjectSharedBufferWriterPool>true</InjectSharedBufferWriterPool>
2019
<InjectSharedNumericExtensions>false</InjectSharedNumericExtensions>
21-
<InjectExperimentalAttributeOnLegacy>true</InjectExperimentalAttributeOnLegacy>
20+
<InjectSharedThrow>false</InjectSharedThrow>
21+
<InjectTrimAttributesOnLegacy>false</InjectTrimAttributesOnLegacy>
2222
</PropertyGroup>
2323

2424
<PropertyGroup>

src/Libraries/Microsoft.Extensions.Telemetry/Microsoft.Extensions.Telemetry.csproj

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
<PropertyGroup>
99
<ForceLatestDotnetVersions>true</ForceLatestDotnetVersions>
1010
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
11+
<InjectGetOrAddOnLegacy>true</InjectGetOrAddOnLegacy>
1112
<InjectExperimentalAttributeOnLegacy>true</InjectExperimentalAttributeOnLegacy>
12-
<InjectSharedDiagnosticIds>true</InjectSharedDiagnosticIds>
13-
<InjectSharedText>true</InjectSharedText>
1413
<InjectSharedDataValidation>true</InjectSharedDataValidation>
14+
<InjectSharedDiagnosticIds>true</InjectSharedDiagnosticIds>
15+
<InjectSharedLoggingBuffering>true</InjectSharedLoggingBuffering>
16+
<InjectSharedMemoization>true</InjectSharedMemoization>
1517
<InjectSharedNumericExtensions>true</InjectSharedNumericExtensions>
1618
<InjectSharedPools>true</InjectSharedPools>
1719
<InjectSharedRentedSpan>true</InjectSharedRentedSpan>
18-
<InjectSharedMemoization>true</InjectSharedMemoization>
19-
<InjectGetOrAddOnLegacy>true</InjectGetOrAddOnLegacy>
20+
<InjectSharedText>true</InjectSharedText>
2021
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
2122
<UseLoggingGenerator>true</UseLoggingGenerator>
2223
<NoWarn>$(NoWarn);IL2026</NoWarn>

src/Libraries/Microsoft.Extensions.Telemetry/Buffering/DeserializedLogRecord.cs src/Shared/LoggingBuffering/DeserializedLogRecord.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
3-
3+
#if !SHARED_PROJECT || NET9_0_OR_GREATER
44
using System;
55
using System.Collections.Generic;
66
using Microsoft.Extensions.Logging;
@@ -62,3 +62,4 @@ public DeserializedLogRecord(
6262
public override IReadOnlyList<KeyValuePair<string, object?>> Attributes => _attributes;
6363
private IReadOnlyList<KeyValuePair<string, object?>> _attributes;
6464
}
65+
#endif

src/Libraries/Microsoft.Extensions.Telemetry/Buffering/SerializedLogRecord.cs src/Shared/LoggingBuffering/SerializedLogRecord.cs

+16-5
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ namespace Microsoft.Extensions.Diagnostics.Buffering;
1010
/// <summary>
1111
/// Represents a log record that has been serialized for purposes of buffering or similar.
1212
/// </summary>
13-
#pragma warning disable CA1815 // Override equals and operator equals on value types
13+
#pragma warning disable CA1815 // Override equals and operator equals on value types - not used for this struct, would be dead code
1414
internal readonly struct SerializedLogRecord
15-
#pragma warning restore CA1815 // Override equals and operator equals on value types
1615
{
1716
/// <summary>
1817
/// Initializes a new instance of the <see cref="SerializedLogRecord"/> struct.
@@ -41,16 +40,28 @@ public SerializedLogRecord(
4140
serializedAttributes = new List<KeyValuePair<string, object?>>(attributes.Count);
4241
for (int i = 0; i < attributes.Count; i++)
4342
{
43+
string key = attributes[i].Key;
4444
string value = attributes[i].Value?.ToString() ?? string.Empty;
45-
serializedAttributes.Add(new KeyValuePair<string, object?>(attributes[i].Key, value));
45+
serializedAttributes.Add(new KeyValuePair<string, object?>(key, value));
46+
47+
SizeInBytes += key.Length * sizeof(char);
4648
SizeInBytes += value.Length * sizeof(char);
4749
}
48-
49-
Exception = exception?.Message;
5050
}
5151

5252
Attributes = serializedAttributes;
53+
54+
Exception = exception?.Message;
55+
if (Exception is not null)
56+
{
57+
SizeInBytes += Exception.Length * sizeof(char);
58+
}
59+
5360
FormattedMessage = formattedMessage;
61+
if (FormattedMessage is not null)
62+
{
63+
SizeInBytes += FormattedMessage.Length * sizeof(char);
64+
}
5465
}
5566

5667
/// <summary>

src/Shared/Shared.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<ItemGroup>
3030
<PackageReference Include="Microsoft.Bcl.HashCode" Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))" />
3131
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
32+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
3233
<PackageReference Include="Microsoft.Extensions.ObjectPool" />
3334
<PackageReference Include="System.ComponentModel.Annotations" Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))" NoWarn="RT0003" />
3435
<PackageReference Include="System.Memory" Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))" />

test/Libraries/Microsoft.Extensions.Telemetry.Tests/Buffering/GlobalBufferLoggerBuilderExtensionsTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void AddGlobalBuffer_WithConfiguration_RegistersInDI()
4444
{
4545
List<BufferFilterRule> expectedData =
4646
[
47-
new BufferFilterRule("Program.MyLogger", LogLevel.Information, 1, [new("region", "westus2")]),
47+
new BufferFilterRule("Program.MyLogger", LogLevel.Information, 1, [new("region", "westus2"), new ("priority", 1)]),
4848
new BufferFilterRule(null, LogLevel.Information, null, null),
4949
];
5050
ConfigurationBuilder configBuilder = new ConfigurationBuilder();

test/Libraries/Microsoft.Extensions.Telemetry.Tests/Buffering/LoggerFilterRuleSelectorTests.cs

+18
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,22 @@ public void WhenManyRuleApply_SelectsLast()
6868
// Assert
6969
Assert.Same(rules.Last(), actualResult);
7070
}
71+
72+
[Fact]
73+
public void CanWorkWithValueTypeAttributes()
74+
{
75+
// Arrange
76+
var rules = new List<BufferFilterRule>
77+
{
78+
new BufferFilterRule("Program.MyLogger", LogLevel.Warning, 1, [new("priority", 2)]), // the best rule
79+
new BufferFilterRule("Program.MyLogger", LogLevel.Warning, 1, [new("priority", 3)]),
80+
new BufferFilterRule("Program.MyLogger*", LogLevel.Warning, 1, null),
81+
};
82+
83+
// Act
84+
BufferFilterRuleSelector.Select(rules, "Program.MyLogger", LogLevel.Warning, 1, [new("priority", 2)], out var actualResult);
85+
86+
// Assert
87+
Assert.Same(rules.First(), actualResult);
88+
}
7189
}

test/Libraries/Microsoft.Extensions.Telemetry.Tests/appsettings.json

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
{
3030
"key": "region",
3131
"value": "westus2"
32+
},
33+
{
34+
"key": "priority",
35+
"value": 1
3236
}
3337
]
3438
},

0 commit comments

Comments
 (0)