Skip to content

Commit

Permalink
Add DiagnosticSource.Write<T> API to assist with trimming
Browse files Browse the repository at this point in the history
  • Loading branch information
eerhardt committed Sep 29, 2022
1 parent ebaba40 commit e65bd6d
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ internal static DiagnosticListener LogHostBuilding(HostApplicationBuilder hostAp

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern",
Justification = "The values being passed into Write are being consumed by the application already.")]
private static void Write<T>(
private static void Write<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(
DiagnosticSource diagnosticSource,
string name,
T value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ protected DiagnosticSource() { }
public virtual bool IsEnabled(string name, object? arg1, object? arg2 = null) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("The type of object being written to DiagnosticSource cannot be discovered statically.")]
public abstract void Write(string name, object? value);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("The type of object being written to DiagnosticSource cannot be discovered statically.")]
public void Write<[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)] T>(string name, T? value) { }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<CLSCompliant>false</CLSCompliant>
Expand All @@ -11,6 +11,8 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMemberTypes.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMembersAttribute.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\RequiresUnreferencedCodeAttribute.cs" />
<Compile Include="$(CoreLibSharedDir)System\Runtime\CompilerServices\IsExternalInit.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,11 @@ public virtual void OnActivityImport(System.Diagnostics.Activity activity, objec
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("The type of object being written to DiagnosticSource cannot be discovered statically.")]
public System.Diagnostics.Activity StartActivity(System.Diagnostics.Activity activity, object? args) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("The type of object being written to DiagnosticSource cannot be discovered statically.")]
public System.Diagnostics.Activity StartActivity<[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)] T>(Activity activity, T? args) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("The type of object being written to DiagnosticSource cannot be discovered statically.")]
public void StopActivity(System.Diagnostics.Activity activity, object? args) { }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("The type of object being written to DiagnosticSource cannot be discovered statically.")]
public void StopActivity<[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)] T>(Activity activity, T? args) { throw null; }
}
public enum ActivitySamplingResult
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ System.Diagnostics.DiagnosticSource</PackageDescription>

<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicDependencyAttribute.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMemberTypes.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMembersAttribute.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\RequiresUnreferencedCodeAttribute.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\UnconditionalSuppressMessageAttribute.cs" />
<Compile Include="$(CoreLibSharedDir)System\Runtime\CompilerServices\IsExternalInit.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public abstract partial class DiagnosticSource
[RequiresUnreferencedCode(WriteRequiresUnreferencedCode)]
public abstract void Write(string name, object? value);

/// <inheritdoc cref="Write"/>
/// <typeparam name="T">The type of the value being passed as a payload for the event.</typeparam>
[RequiresUnreferencedCode(WriteRequiresUnreferencedCode)]
public void Write<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(string name, T? value) =>
Write(name, (object?)value);

/// <summary>
/// Optional: if there is expensive setup for the notification, you can call IsEnabled
/// before doing this setup. Consumers should not be assuming that they only get notifications
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public Activity StartActivity(Activity activity, object? args)
return activity;
}

/// <inheritdoc cref="StartActivity"/>
/// <typeparam name="T">The type of the value being passed as a payload for the event.</typeparam>
[RequiresUnreferencedCode(WriteRequiresUnreferencedCode)]
public Activity StartActivity<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(Activity activity, T? args)
=> StartActivity(activity, (object?)args);

/// <summary>
/// Stops given Activity: maintains global Current Activity and notifies consumers
/// that Activity was stopped. Consumers could access <see cref="Activity.Current"/>
Expand All @@ -54,6 +60,12 @@ public void StopActivity(Activity activity, object? args)
activity.Stop(); // Resets Activity.Current (we want this after the Write)
}

/// <inheritdoc cref="StartActivity"/>
/// <typeparam name="T">The type of the value being passed as a payload for the event.</typeparam>
[RequiresUnreferencedCode(WriteRequiresUnreferencedCode)]
public void StopActivity<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(Activity activity, T? args)
=> StopActivity(activity, (object?)args);

/// <summary>
/// Optional: If an instrumentation site creating an new activity that was caused
/// by something outside the process (e.g. an incoming HTTP request), then that site
Expand Down

0 comments on commit e65bd6d

Please sign in to comment.