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

Trimming and AOT compatibility #1411

Merged
merged 1 commit into from
Dec 27, 2023
Merged
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
1 change: 1 addition & 0 deletions projects/RabbitMQ.Client/RabbitMQ.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
<AssemblyTitle>RabbitMQ Client Library for .NET</AssemblyTitle>
<Authors>VMware</Authors>
<Company>VMware, Inc. or its affiliates.</Company>
Expand Down
9 changes: 8 additions & 1 deletion projects/RabbitMQ.Client/client/api/ICredentialsRefresher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

using System;
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Tracing;
namespace RabbitMQ.Client
{
Expand All @@ -39,7 +40,7 @@ public interface ICredentialsRefresher
ICredentialsProvider Register(ICredentialsProvider provider, NotifyCredentialRefreshed callback);
bool Unregister(ICredentialsProvider provider);

delegate void NotifyCredentialRefreshed(bool succesfully);
delegate void NotifyCredentialRefreshed(bool successfully);
}

[EventSource(Name = "TimerBasedCredentialRefresher")]
Expand All @@ -52,10 +53,16 @@ public class TimerBasedCredentialRefresherEventSource : EventSource
[Event(2)]
public void Unregistered(string name) => WriteEvent(2, "UnRegistered", name);
[Event(3)]
#if NET6_0_OR_GREATER
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Parameters to this method are primitive and are trimmer safe")]
#endif
public void ScheduledTimer(string name, double interval) => WriteEvent(3, "ScheduledTimer", name, interval);
[Event(4)]
public void TriggeredTimer(string name) => WriteEvent(4, "TriggeredTimer", name);
[Event(5)]
#if NET6_0_OR_GREATER
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Parameters to this method are primitive and are trimmer safe")]
#endif
public void RefreshedCredentials(string name, bool succesfully) => WriteEvent(5, "RefreshedCredentials", name, succesfully);
[Event(6)]
public void AlreadyRegistered(string name) => WriteEvent(6, "AlreadyRegistered", name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
//---------------------------------------------------------------------------

using System;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Tracing;

namespace RabbitMQ.Client.Logging
Expand Down Expand Up @@ -62,31 +63,30 @@ public void Warn(string message)
if (IsEnabled())
WriteEvent(2, message);
}
#if NET452
[Event(3, Message = "ERROR", Keywords = Keywords.Log, Level = EventLevel.Error)]
public void Error(string message, string detail)
{
if(IsEnabled())
this.WriteEvent(3, message, detail);
}
#else

[Event(3, Message = "ERROR", Keywords = Keywords.Log, Level = EventLevel.Error)]
public void Error(string message, RabbitMqExceptionDetail ex)
{
if (IsEnabled())
{
#if NET6_0_OR_GREATER
WriteExceptionEvent(message, ex);

[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "The properties are preserved with the DynamicallyAccessedMembers attribute.")]
void WriteExceptionEvent<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(string message, T ex)
{
WriteEvent(3, message, ex);
}
#else
WriteEvent(3, message, ex);
}
#endif
}
}

[NonEvent]
public void Error(string message, Exception ex)
{

#if NET452
Error(message, ex.ToString());
#else
Error(message, new RabbitMqExceptionDetail(ex));
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public RabbitMqExceptionDetail(IDictionary<string, object> ex)
}
}

// NOTE: This type is used to write EventData in RabbitMqClientEventSource.Error. To make it trim-compatible, these properties are preserved
// in RabbitMqClientEventSource. If RabbitMqExceptionDetail gets a property that is a complex type, we need to ensure the nested properties are
// preserved as well.

public string Type { get; }
public string Message { get; }
public string StackTrace { get; }
Expand Down