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

Annotating System.Diagnostics.DiagnosticSource on AOT #72706

Merged
merged 7 commits into from
Aug 3, 2022
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 @@ -8,6 +8,7 @@
<IsPackable>true</IsPackable>
<!-- Lifetime rules introduced in C# 11 impact scenarios in net6 framework -->
<LangVersion Condition="'$(TargetFramework)' == 'net6.0'">10</LangVersion>
<EnableAOTAnalyzer>true</EnableAOTAnalyzer>
<PackageDescription>Provides Classes that allow you to decouple code logging rich (unserializable) diagnostics/telemetry (e.g. framework) from code that consumes it (e.g. tools)

Commonly Used Types:
Expand Down Expand Up @@ -103,6 +104,10 @@ System.Diagnostics.DiagnosticSource</PackageDescription>
<Compile Include="$(CoreLibSharedDir)System\Runtime\CompilerServices\IsExternalInit.cs" />
</ItemGroup>

<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))" >
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\RequiresDynamicCodeAttribute.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' or
'$(TargetFramework)' == 'netstandard2.0'">
<Compile Include="System\Diagnostics\Activity.DateTime.corefx.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,8 @@ public FilterAndTransform(string filterAndPayloadSpec, int startIdx, int endIdx,
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2119",
Justification = "DAM on EventSource references this compiler-generated local function which calls a " +
"method that requires unreferenced code. EventSource will not access this local function.")]
[UnconditionalSuppressMessage("AotAnalysis", "IL3050:RequiresDynamicCode",
Justification = "DiagnosticSource.Write is marked with RequiresUnreferencedCode.")]
LakshanF marked this conversation as resolved.
Show resolved Hide resolved
void OnEventWritten(KeyValuePair<string, object?> evnt)
{
// The filter given to the DiagnosticSource may not work if users don't is 'IsEnabled' as expected.
Expand Down Expand Up @@ -888,6 +890,8 @@ internal static void CreateActivityListener(DiagnosticSourceEventSource eventSou
[DynamicDependency(nameof(TimeSpan.Ticks), typeof(TimeSpan))]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "Activity's properties are being preserved with the DynamicDependencies on OnActivityStarted.")]
[UnconditionalSuppressMessage("AotAnalysis", "IL3050:RequiresDynamicCode",
Justification = "Activity's properties are being preserved with the DynamicDependencies on OnActivityStarted.")]
LakshanF marked this conversation as resolved.
Show resolved Hide resolved
private static void OnActivityStarted(DiagnosticSourceEventSource eventSource, Activity activity)
{
FilterAndTransform? list = eventSource._activitySourceSpecs;
Expand All @@ -907,6 +911,8 @@ private static void OnActivityStarted(DiagnosticSourceEventSource eventSource, A

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "Activity's properties are being preserved with the DynamicDependencies on OnActivityStarted.")]
[UnconditionalSuppressMessage("AotAnalysis", "IL3050:RequiresDynamicCode",
Justification = "Activity's properties are being preserved with the DynamicDependencies on OnActivityStarted.")]
private static void OnActivityStopped(DiagnosticSourceEventSource eventSource, Activity activity)
{
FilterAndTransform? list = eventSource._activitySourceSpecs;
Expand Down Expand Up @@ -1009,6 +1015,7 @@ private void Dispose()
Justification = "In EventSource, EnsureDescriptorsInitialized's use of GetType preserves this method which " +
"requires unreferenced code, but EnsureDescriptorsInitialized does not access this member and is safe to call.")]
[RequiresUnreferencedCode(DiagnosticSource.WriteRequiresUnreferencedCode)]
[RequiresDynamicCode(DiagnosticSource.WriteRequiresUnreferencedCode)]
LakshanF marked this conversation as resolved.
Show resolved Hide resolved
public List<KeyValuePair<string, string?>> Morph(object? args)
{
// Transform the args into a bag of key-value strings.
Expand Down Expand Up @@ -1190,6 +1197,7 @@ public TransformSpec(string transformSpec, int startIdx, int endIdx, TransformSp
Justification = "In EventSource, EnsureDescriptorsInitialized's use of GetType preserves this method which " +
"requires unreferenced code, but EnsureDescriptorsInitialized does not access this member and is safe to call.")]
[RequiresUnreferencedCode(DiagnosticSource.WriteRequiresUnreferencedCode)]
[RequiresDynamicCode(DiagnosticSource.WriteRequiresUnreferencedCode)]
public KeyValuePair<string, string?> Morph(object? obj)
{
for (PropertySpec? cur = _fetches; cur != null; cur = cur.Next)
Expand Down Expand Up @@ -1244,6 +1252,7 @@ public PropertySpec(string propertyName, PropertySpec? next)
Justification = "In EventSource, EnsureDescriptorsInitialized's use of GetType preserves this method which " +
"requires unreferenced code, but EnsureDescriptorsInitialized does not access this member and is safe to call.")]
[RequiresUnreferencedCode(DiagnosticSource.WriteRequiresUnreferencedCode)]
[RequiresDynamicCode(DiagnosticSource.WriteRequiresUnreferencedCode)]
public object? Fetch(object? obj)
{
PropertyFetch? fetch = _fetchForExpectedType;
Expand Down Expand Up @@ -1290,6 +1299,7 @@ public PropertyFetch(Type? type)
Justification = "In EventSource, EnsureDescriptorsInitialized's use of GetType preserves this method which " +
"requires unreferenced code, but EnsureDescriptorsInitialized does not access this member and is safe to call.")]
[RequiresUnreferencedCode(DiagnosticSource.WriteRequiresUnreferencedCode)]
[RequiresDynamicCode(DiagnosticSource.WriteRequiresUnreferencedCode)]
public static PropertyFetch FetcherForProperty(Type? type, string propertyName)
{
if (propertyName == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Versioning;
using System.Security;
using System.Threading;
Expand Down Expand Up @@ -249,6 +250,8 @@ private void RemoveInstrumentState(Instrument instrument, InstrumentState state)
return instrumentState;
}

[UnconditionalSuppressMessage("AotAnalysis", "IL3050:RequiresDynamicCode",
Justification = "MakeGenericType is creating instances over reference types that works fine in AOT.")]
internal InstrumentState? BuildInstrumentState(Instrument instrument)
{
Func<Aggregator?>? createAggregatorFunc = GetAggregatorFactory(instrument);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,7 @@ private void AssertActivityMatchesEvent(Activity a, DiagnosticSourceEvent e, boo
}

[Fact]
[ActiveIssue("https:/dotnet/runtime/issues/72801", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))]
public void NoExceptionThrownWhenProcessingStaticActivityProperties()
{
// Ensures that no exception is thrown when static properties on the Activity type are passed to EventListener.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
<PropertyGroup>
<DefineConstants Condition="$([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')) == '.NETCoreApp'">$(DefineConstants);MEMORYMARSHAL_SUPPORT</DefineConstants>
</PropertyGroup>
<ItemGroup>
<RdXmlFile Include="default.rd.xml" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
<Compile Include="DiagnosticSourceEventSourceBridgeTests.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Directives>
<Application>
<Assembly Name="mscorlib" />
<Assembly Name="System.Runtime" />
<Assembly Name="System.Private.CoreLib">
<Type Name="System.IEquatable`1[[System.Diagnostics.ActivityLink, System.Diagnostics.DiagnosticSource]]">
<Method Name="Equals" Dynamic="Required All">
</Method>
</Type>
</Assembly>
<Assembly Name="System.Diagnostics.DiagnosticSource">
<Type Name="System.Diagnostics.ActivityEvent" Dynamic="Required All" />
<Type Name="System.Diagnostics.Activity+TagsLinkedList" Dynamic="Required All" />
<Type Name="System.Diagnostics.DiagLinkedList`1[[System.Diagnostics.ActivityEvent, System.Diagnostics.DiagnosticSource]]" Dynamic="Required All" />
</Assembly>
</Application>
</Directives>
1 change: 0 additions & 1 deletion src/libraries/tests.proj
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,6 @@
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Security.Cryptography.Xml\tests\System.Security.Cryptography.Xml.Tests.csproj" />
<ProjectExclusions Include="$(MSBuildThisFileDirectory)Microsoft.Extensions.Logging.EventSource\tests\Microsoft.Extensions.Logging.EventSource.Tests.csproj" />
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.ComponentModel.Annotations\tests\System.ComponentModel.Annotations.Tests.csproj" />
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Diagnostics.DiagnosticSource\tests\System.Diagnostics.DiagnosticSource.Tests.csproj" />
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Threading.RateLimiting\tests\System.Threading.RateLimiting.Tests.csproj" />
<ProjectExclusions Include="$(MSBuildThisFileDirectory)Microsoft.Extensions.Caching.Memory\tests\Microsoft.Extensions.Caching.Memory.Tests.csproj" />
<ProjectExclusions Include="$(MSBuildThisFileDirectory)Microsoft.Extensions.Configuration.Xml\tests\Microsoft.Extensions.Configuration.Xml.Tests.csproj" />
Expand Down