Skip to content

Commit 91bae52

Browse files
eerhardtlukebakken
authored andcommitted
Trimming and AOT compatibility
Resolve the trim warning coming from RabbitMqClientEventSource.Error. Preserve the properties of RabbitMqExceptionDetail and suppress the warning. The TimerBasedCredentialRefresherEventSource has warnings as well, but these can be suppressed because the parameters are primitive. In .NET 8 these warnings no longer need to be suppressed because of dotnet/runtime#83751. Fix #1410
1 parent bded97c commit 91bae52

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

projects/RabbitMQ.Client/RabbitMQ.Client.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
55
<NoWarn>$(NoWarn);CS1591</NoWarn>
66
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
7+
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
78
<AssemblyTitle>RabbitMQ Client Library for .NET</AssemblyTitle>
89
<Authors>VMware</Authors>
910
<Company>VMware, Inc. or its affiliates.</Company>

projects/RabbitMQ.Client/client/api/ICredentialsRefresher.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
using System;
3333
using System.Collections.Concurrent;
34+
using System.Diagnostics.CodeAnalysis;
3435
using System.Diagnostics.Tracing;
3536
namespace RabbitMQ.Client
3637
{
@@ -39,7 +40,7 @@ public interface ICredentialsRefresher
3940
ICredentialsProvider Register(ICredentialsProvider provider, NotifyCredentialRefreshed callback);
4041
bool Unregister(ICredentialsProvider provider);
4142

42-
delegate void NotifyCredentialRefreshed(bool succesfully);
43+
delegate void NotifyCredentialRefreshed(bool successfully);
4344
}
4445

4546
[EventSource(Name = "TimerBasedCredentialRefresher")]
@@ -52,10 +53,16 @@ public class TimerBasedCredentialRefresherEventSource : EventSource
5253
[Event(2)]
5354
public void Unregistered(string name) => WriteEvent(2, "UnRegistered", name);
5455
[Event(3)]
56+
#if NET6_0_OR_GREATER
57+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Parameters to this method are primitive and are trimmer safe")]
58+
#endif
5559
public void ScheduledTimer(string name, double interval) => WriteEvent(3, "ScheduledTimer", name, interval);
5660
[Event(4)]
5761
public void TriggeredTimer(string name) => WriteEvent(4, "TriggeredTimer", name);
5862
[Event(5)]
63+
#if NET6_0_OR_GREATER
64+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Parameters to this method are primitive and are trimmer safe")]
65+
#endif
5966
public void RefreshedCredentials(string name, bool succesfully) => WriteEvent(5, "RefreshedCredentials", name, succesfully);
6067
[Event(6)]
6168
public void AlreadyRegistered(string name) => WriteEvent(6, "AlreadyRegistered", name);

projects/RabbitMQ.Client/client/logging/RabbitMqClientEventSource.cs

+14-14
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
//---------------------------------------------------------------------------
3131

3232
using System;
33+
using System.Diagnostics.CodeAnalysis;
3334
using System.Diagnostics.Tracing;
3435

3536
namespace RabbitMQ.Client.Logging
@@ -62,31 +63,30 @@ public void Warn(string message)
6263
if (IsEnabled())
6364
WriteEvent(2, message);
6465
}
65-
#if NET452
66-
[Event(3, Message = "ERROR", Keywords = Keywords.Log, Level = EventLevel.Error)]
67-
public void Error(string message, string detail)
68-
{
69-
if(IsEnabled())
70-
this.WriteEvent(3, message, detail);
71-
}
72-
#else
66+
7367
[Event(3, Message = "ERROR", Keywords = Keywords.Log, Level = EventLevel.Error)]
7468
public void Error(string message, RabbitMqExceptionDetail ex)
7569
{
7670
if (IsEnabled())
71+
{
72+
#if NET6_0_OR_GREATER
73+
WriteExceptionEvent(message, ex);
74+
75+
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "The properties are preserved with the DynamicallyAccessedMembers attribute.")]
76+
void WriteExceptionEvent<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(string message, T ex)
77+
{
78+
WriteEvent(3, message, ex);
79+
}
80+
#else
7781
WriteEvent(3, message, ex);
78-
}
7982
#endif
83+
}
84+
}
8085

8186
[NonEvent]
8287
public void Error(string message, Exception ex)
8388
{
84-
85-
#if NET452
86-
Error(message, ex.ToString());
87-
#else
8889
Error(message, new RabbitMqExceptionDetail(ex));
89-
#endif
9090
}
9191
}
9292
}

projects/RabbitMQ.Client/client/logging/RabbitMqExceptionDetail.cs

+4
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ public RabbitMqExceptionDetail(IDictionary<string, object> ex)
6060
}
6161
}
6262

63+
// NOTE: This type is used to write EventData in RabbitMqClientEventSource.Error. To make it trim-compatible, these properties are preserved
64+
// in RabbitMqClientEventSource. If RabbitMqExceptionDetail gets a property that is a complex type, we need to ensure the nested properties are
65+
// preserved as well.
66+
6367
public string Type { get; }
6468
public string Message { get; }
6569
public string StackTrace { get; }

0 commit comments

Comments
 (0)