// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
+using System.Collections.Generic;
using System.Diagnostics;
-using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Tracing;
-using System.Runtime.InteropServices;
+using System.Net.Sockets;
using System.Threading;
using Microsoft.Extensions.Internal;
}
}
- private const int MaxIPFormattedLength = 128;
-
[Event(ResolutionStartEventId, Level = EventLevel.Informational)]
private void ResolutionStart(string hostNameOrAddress) => WriteEvent(ResolutionStartEventId, hostNameOrAddress);
public ValueStopwatch BeforeResolution(object hostNameOrAddress)
{
Debug.Assert(hostNameOrAddress != null);
- Debug.Assert(hostNameOrAddress is string || hostNameOrAddress is IPAddress);
+ Debug.Assert(
+ hostNameOrAddress is string ||
+ hostNameOrAddress is IPAddress ||
+ hostNameOrAddress is KeyValuePair<string, AddressFamily> ||
+ hostNameOrAddress is KeyValuePair<IPAddress, AddressFamily>,
+ $"Unknown hostNameOrAddress type: {hostNameOrAddress.GetType().Name}");
if (IsEnabled())
{
if (IsEnabled(EventLevel.Informational, EventKeywords.None))
{
- if (hostNameOrAddress is string s)
- {
- ResolutionStart(s);
- }
- else
+ string host = hostNameOrAddress switch
{
- WriteEvent(ResolutionStartEventId, FormatIPAddressNullTerminated((IPAddress)hostNameOrAddress, stackalloc char[MaxIPFormattedLength]));
- }
+ string h => h,
+ KeyValuePair<string, AddressFamily> t => t.Key,
+ IPAddress a => a.ToString(),
+ KeyValuePair<IPAddress, AddressFamily> t => t.Key.ToString(),
+ _ => null!
+ };
+ ResolutionStart(host);
}
return ValueStopwatch.StartNew();
}
}
}
-
- [NonEvent]
- private static Span<char> FormatIPAddressNullTerminated(IPAddress address, Span<char> destination)
- {
- Debug.Assert(address != null);
-
- bool success = address.TryFormat(destination, out int charsWritten);
- Debug.Assert(success);
-
- Debug.Assert(charsWritten < destination.Length);
- destination[charsWritten] = '\0';
-
- return destination.Slice(0, charsWritten + 1);
- }
-
-
- // WriteEvent overloads taking Span<char> are imitating string arguments
- // Span arguments are expected to be null-terminated
-
-#if !ES_BUILD_STANDALONE
- [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern",
- Justification = "Parameters to this method are primitive and are trimmer safe")]
-#endif
- [NonEvent]
- private unsafe void WriteEvent(int eventId, Span<char> arg1)
- {
- Debug.Assert(!arg1.IsEmpty && arg1.IndexOf('\0') == arg1.Length - 1, "Expecting a null-terminated ROS<char>");
-
- if (IsEnabled())
- {
- fixed (char* arg1Ptr = &MemoryMarshal.GetReference(arg1))
- {
- EventData descr = new EventData
- {
- DataPointer = (IntPtr)(arg1Ptr),
- Size = arg1.Length * sizeof(char)
- };
-
- WriteEventCore(eventId, eventDataCount: 1, &descr);
- }
- }
- }
}
}