Skip to content

Commit f3c8087

Browse files
authored
Add suppression for IL3050 when serializing objects for structured logging. (#1795)
The serialization is done as a best attempt with a fallback to using "ToString" if the reflection fails for NativeAOT.
1 parent e36bd5d commit f3c8087

File tree

6 files changed

+203
-1
lines changed

6 files changed

+203
-1
lines changed

Libraries/src/Amazon.Lambda.RuntimeSupport/Amazon.Lambda.RuntimeSupport.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
<WarningsAsErrors>IL2026,IL2067,IL2075</WarningsAsErrors>
2525
<IsTrimmable>true</IsTrimmable>
2626
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
27-
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
2827
</PropertyGroup>
2928

3029
<ItemGroup>

Libraries/src/Amazon.Lambda.RuntimeSupport/Helpers/Logging/JsonLogMessageFormatter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ private void WriteException(Utf8JsonWriter writer, MessageState state)
208208
/// <param name="directive"></param>
209209
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026",
210210
Justification = "If formatting an object using JSON serialization this will do its best attempt. If the object has trim errors formatting will fall back to ToString for the object.")]
211+
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("ReflectionAnalysis", "IL3050",
212+
Justification = "If formatting an object using JSON serialization this will do its best attempt. If the object has trim errors formatting will fall back to ToString for the object.")]
211213
private void FormatJsonValue(Utf8JsonWriter writer, object value, string formatArguments, MessageProperty.Directive directive)
212214
{
213215
if(value == null)
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Text;
4+
using System.Reflection;
5+
6+
using Xunit;
7+
using Xunit.Abstractions;
8+
using System.IO;
9+
10+
#if NET8_0_OR_GREATER
11+
namespace Amazon.Lambda.RuntimeSupport.UnitTests
12+
{
13+
public class NativeAOTTests
14+
{
15+
private ITestOutputHelper _output;
16+
17+
public NativeAOTTests(ITestOutputHelper output)
18+
{
19+
_output = output;
20+
}
21+
22+
[Fact]
23+
public void EnsureNoTrimWarningsDuringPublish()
24+
{
25+
var projectDirectory = FindProject("NativeAOTFunction");
26+
27+
_output.WriteLine("dotnet publish " + projectDirectory);
28+
var output = ExecutePublish(projectDirectory);
29+
_output.WriteLine(output.Log);
30+
31+
Assert.True(output.ExitCode == 0);
32+
Assert.DoesNotContain("AOT analysis warning", output.Log);
33+
}
34+
35+
private (int ExitCode, string Log) ExecutePublish(string projectDirectory)
36+
{
37+
var buffer = new StringBuilder();
38+
var handler = (DataReceivedEventHandler)((o, e) =>
39+
{
40+
if (string.IsNullOrEmpty(e.Data))
41+
return;
42+
43+
buffer.AppendLine(e.Data);
44+
});
45+
46+
var startInfo = new ProcessStartInfo
47+
{
48+
FileName = "dotnet",
49+
Arguments = $"publish",
50+
WorkingDirectory = projectDirectory,
51+
RedirectStandardOutput = true,
52+
RedirectStandardError = true,
53+
UseShellExecute = false,
54+
CreateNoWindow = true
55+
};
56+
57+
int exitCode;
58+
using (var proc = new Process())
59+
{
60+
proc.StartInfo = startInfo;
61+
proc.Start();
62+
63+
if (startInfo.RedirectStandardOutput)
64+
{
65+
proc.ErrorDataReceived += handler;
66+
proc.OutputDataReceived += handler;
67+
proc.BeginOutputReadLine();
68+
proc.BeginErrorReadLine();
69+
70+
proc.EnableRaisingEvents = true;
71+
}
72+
73+
proc.WaitForExit();
74+
75+
76+
exitCode = proc.ExitCode;
77+
}
78+
79+
return (exitCode, buffer.ToString());
80+
}
81+
82+
private string FindProject(string projectName)
83+
{
84+
var directory = Assembly.GetExecutingAssembly().Location;
85+
while (directory != null)
86+
{
87+
var fullpath = Path.Combine(directory, projectName);
88+
if (Directory.Exists(fullpath))
89+
{
90+
return fullpath;
91+
}
92+
93+
directory = Directory.GetParent(directory)?.FullName;
94+
}
95+
96+
throw new Exception("Failed to find project directory " + projectName);
97+
}
98+
}
99+
}
100+
#endif
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using Amazon.Lambda.Core;
2+
using Amazon.Lambda.RuntimeSupport;
3+
using Amazon.Lambda.Serialization.SystemTextJson;
4+
using System.Text.Json.Serialization;
5+
6+
namespace StructureAOTTest;
7+
8+
public class Function
9+
{
10+
/// <summary>
11+
/// The main entry point for the Lambda function. The main function is called once during the Lambda init phase. It
12+
/// initializes the .NET Lambda runtime client passing in the function handler to invoke for each Lambda event and
13+
/// the JSON serializer to use for converting Lambda JSON format to the .NET types.
14+
/// </summary>
15+
private static async Task Main()
16+
{
17+
Func<string, ILambdaContext, string> handler = FunctionHandler;
18+
await LambdaBootstrapBuilder.Create(handler, new SourceGeneratorLambdaJsonSerializer<LambdaFunctionJsonSerializerContext>())
19+
.Build()
20+
.RunAsync();
21+
}
22+
23+
/// <summary>
24+
/// A simple function that takes a string and does a ToUpper.
25+
///
26+
/// To use this handler to respond to an AWS event, reference the appropriate package from
27+
/// https://github.com/aws/aws-lambda-dotnet#events
28+
/// and change the string input parameter to the desired event type. When the event type
29+
/// is changed, the handler type registered in the main method needs to be updated and the LambdaFunctionJsonSerializerContext
30+
/// defined below will need the JsonSerializable updated. If the return type and event type are different then the
31+
/// LambdaFunctionJsonSerializerContext must have two JsonSerializable attributes, one for each type.
32+
///
33+
// When using Native AOT extra testing with the deployed Lambda functions is required to ensure
34+
// the libraries used in the Lambda function work correctly with Native AOT. If a runtime
35+
// error occurs about missing types or methods the most likely solution will be to remove references to trim-unsafe
36+
// code or configure trimming options. This sample defaults to partial TrimMode because currently the AWS
37+
// SDK for .NET does not support trimming. This will result in a larger executable size, and still does not
38+
// guarantee runtime trimming errors won't be hit.
39+
/// </summary>
40+
/// <param name="input">The event for the Lambda function handler to process.</param>
41+
/// <param name="context">The ILambdaContext that provides methods for logging and describing the Lambda environment.</param>
42+
/// <returns></returns>
43+
public static string FunctionHandler(string input, ILambdaContext context)
44+
{
45+
return input.ToUpper();
46+
}
47+
}
48+
49+
/// <summary>
50+
/// This class is used to register the input event and return type for the FunctionHandler method with the System.Text.Json source generator.
51+
/// There must be a JsonSerializable attribute for each type used as the input and return type or a runtime error will occur
52+
/// from the JSON serializer unable to find the serialization information for unknown types.
53+
/// </summary>
54+
[JsonSerializable(typeof(string))]
55+
public partial class LambdaFunctionJsonSerializerContext : JsonSerializerContext
56+
{
57+
// By using this partial class derived from JsonSerializerContext, we can generate reflection free JSON Serializer code at compile time
58+
// which can deserialize our class and properties. However, we must attribute this class to tell it what types to generate serialization code for.
59+
// See https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-source-generation
60+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<AWSProjectType>Lambda</AWSProjectType>
8+
<!-- This property makes the build directory similar to a publish directory and helps the AWS .NET Lambda Mock Test Tool find project dependencies. -->
9+
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
10+
<!-- Generate Native AOT image during publishing to improve cold start time. -->
11+
<PublishAot>true</PublishAot>
12+
<!-- StripSymbols tells the compiler to strip debugging symbols from the final executable if we're on Linux and put them into their own file.
13+
This will greatly reduce the final executable's size.-->
14+
<StripSymbols>true</StripSymbols>
15+
<!-- TrimMode partial will only trim assemblies marked as trimmable. To reduce package size make all assemblies trimmable and set TrimMode to full.
16+
If there are trim warnings during build, you can hit errors at runtime.-->
17+
<TrimMode>partial</TrimMode>
18+
<TrimmerSingleWarn>false</TrimmerSingleWarn>
19+
</PropertyGroup>
20+
<ItemGroup>
21+
<ProjectReference Include="..\..\src\Amazon.Lambda.Core\Amazon.Lambda.Core.csproj" />
22+
<ProjectReference Include="..\..\src\Amazon.Lambda.RuntimeSupport\Amazon.Lambda.RuntimeSupport.csproj" />
23+
<ProjectReference Include="..\..\src\Amazon.Lambda.Serialization.SystemTextJson\Amazon.Lambda.Serialization.SystemTextJson.csproj" />
24+
</ItemGroup>
25+
</Project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"Information": [
3+
"This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.",
4+
"To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.",
5+
"dotnet lambda help",
6+
"All the command line options for the Lambda command can be specified in this file."
7+
],
8+
"profile": "default",
9+
"region": "us-west-2",
10+
"configuration": "Release",
11+
"function-runtime": "dotnet8",
12+
"function-memory-size": 512,
13+
"function-timeout": 30,
14+
"function-handler": "NativeAOTFunction",
15+
"msbuild-parameters": "--self-contained true"
16+
}

0 commit comments

Comments
 (0)