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

Enable AoT and trimming analyzers #2008

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions src/Serilog/Configuration/LoggerSettingsConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public LoggerConfiguration Settings(ILoggerSettings settings)
/// <returns>Configuration object allowing method chaining.</returns>
/// <remarks>In case of duplicate keys, the last value for the key is kept and the previous ones are ignored.</remarks>
/// <exception cref="ArgumentNullException">When <paramref name="settings"/> is <code>null</code></exception>
[RequiresDynamicCode("KeyValuePair scans for configuration assemblies at run time and is not compatible with trimming.")]
[RequiresUnreferencedCode("KeyValuePair scans for configuration assemblies at run time and is not compatible with trimming.")]
[RequiresDynamicCode("KeyValuePair may need to create arrays, which requires dynamic code generation and is not compatible with AOT.")]
public LoggerConfiguration KeyValuePairs(IEnumerable<KeyValuePair<string, string>> settings)
Expand All @@ -60,6 +61,7 @@ public LoggerConfiguration KeyValuePairs(IEnumerable<KeyValuePair<string, string
return KeyValuePairs(uniqueSettings);
}

[RequiresDynamicCode("KeyValuePair scans for configuration settings at run time.")]
[RequiresUnreferencedCode("KeyValuePair scans for configuration settings at run time.")]
[RequiresDynamicCode("Creates arrays of unknown element type")]
LoggerConfiguration KeyValuePairs(IReadOnlyDictionary<string, string> settings)
Expand Down
8 changes: 8 additions & 0 deletions src/Serilog/Serilog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">
<EnableAotAnalyzer>true</EnableAotAnalyzer>
<EnableSingleFileAnalyzer>true</EnableSingleFileAnalyzer>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
<IsAotCompatible>true</IsAotCompatible>
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>

<!-- `PropertyGroup` per TFM: -->

<PropertyGroup Condition=" '$(TargetFramework)' == 'net462' ">
Expand Down
7 changes: 6 additions & 1 deletion src/Serilog/Settings/KeyValuePairs/KeyValuePairSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@

namespace Serilog.Settings.KeyValuePairs;

[RequiresUnreferencedCode("Scans assemblies at runtime")] // RequiresUnreferencedCode couldn't be applied to types in .NET 5.
#if NET6_0_OR_GREATER
[RequiresDynamicCode("Creates arrays of unknown element type")]
[RequiresUnreferencedCode("Scans assemblies at runtime")]
#endif
class KeyValuePairSettings : ILoggerSettings
{
const string UsingDirective = "using";
Expand Down Expand Up @@ -161,6 +163,7 @@ internal static bool IsValidSwitchName(string input)
return Regex.IsMatch(input, LevelSwitchNameRegex);
}

[RequiresDynamicCode("Reflects against accessors using dynamic string")]
[RequiresUnreferencedCode("Reflects against accessors using dynamic string")]
static IReadOnlyDictionary<string, LoggingLevelSwitch> ParseNamedLevelSwitchDeclarationDirectives(IReadOnlyDictionary<string, string> directives)
{
Expand Down Expand Up @@ -211,6 +214,7 @@ static LoggingLevelSwitch LookUpSwitchByName(string switchName, IReadOnlyDiction
throw new InvalidOperationException($"No LoggingLevelSwitch has been declared with name \"{switchName}\". You might be missing a key \"{LevelSwitchDirective}:{switchName}\"");
}

[RequiresDynamicCode("Finds accessors by name")]
[RequiresUnreferencedCode("Finds accessors by name")]
[RequiresDynamicCode("Creates arrays of unknown element type")]
static object ConvertOrLookupByName(string valueOrSwitchName, Type type, IReadOnlyDictionary<string, LoggingLevelSwitch> declaredSwitches)
Expand All @@ -222,6 +226,7 @@ static object ConvertOrLookupByName(string valueOrSwitchName, Type type, IReadOn
return SettingValueConversions.ConvertToType(valueOrSwitchName, type)!;
}

[RequiresDynamicCode("Finds accessors by name")]
[RequiresUnreferencedCode("Finds accessors by name")]
[RequiresDynamicCode("Creates arrays of unknown element type")]
static void ApplyDirectives(List<IGrouping<string, ConfigurationMethodCall>> directives, IList<MethodInfo> configurationMethods, object loggerConfigMethod, IReadOnlyDictionary<string, LoggingLevelSwitch> declaredSwitches)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class SettingValueConversions
},
};

[RequiresDynamicCode("Finds accessors by name")]
[RequiresUnreferencedCode("Finds accessors by name")]
[RequiresDynamicCode("Creates arrays of unknown element type")]
public static object? ConvertToType(string value, Type toType)
Expand Down