Repository: dotnet/runtime
File: src/libraries/System.Net.NameResolution/src/System/Net/Dns.cs (C#)

Code before:
                    _ => null
                }, asyncState);
            }
            else
            {
                return RunAsync(s => s switch
                {
                    string h => GetHostEntryCore(h, AddressFamily.Unspecified),
                    KeyValuePair<string, AddressFamily> t => GetHostEntryCore(t.Key, t.Value),
                    IPAddress a => GetHostEntryCore(a, AddressFamily.Unspecified),
                    KeyValuePair<IPAddress, AddressFamily> t => GetHostEntryCore(t.Key, t.Value),
                    _ => null
                }, asyncState);
            }
        }

        private static  bool TryGetAddrInfoWithTelemetryAsync<T>(string hostName, bool justAddresses, AddressFamily addressFamily, CancellationToken cancellationToken, out Task t)
             where T : class
        {
            ValueStopwatch stopwatch = NameResolutionTelemetry.Log.BeforeResolution(hostName);

            try
            {
                if (NameResolutionPal.TryGetAddrInfoAsync(hostName, justAddresses, addressFamily, cancellationToken, out t))
                {
                    t.ContinueWith((resultTask) => {
                        NameResolutionTelemetry.Log.AfterResolution(stopwatch, successful: resultTask.IsCompletedSuccessfully);
                    }, cancellationToken, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);

                    return true;
                }
            }
            catch
            {
                NameResolutionTelemetry.Log.AfterResolution(stopwatch, successful: false);
                throw;
            }

            t = Task.CompletedTask;
            return false;
        }

        private static Task<TResult> RunAsync<TResult>(Func<object, TResult> func, object arg) =>
            Task.Factory.StartNew(func!, arg, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);

        private static IPHostEntry CreateHostEntryForAddress(IPAddress address) =>
            new IPHostEntry
            {
                HostName = address.ToString(),
                Aliases = Array.Empty<string>(),
                AddressList = new IPAddress[] { address }

Review comment (suggestion):
We should be returning the Task returned by `ContinueWith`, or the ActivityId following the Dns resolution wouldn't reflect the Stop event.
This was the issue with the original Telemetry instrumentation of Dns, pointed out by @ManickaP in https://github.com/dotnet/runtime/issues/41670#issuecomment-685038205 (and fixed in #42188).

Code after:
                    KeyValuePair<IPAddress, AddressFamily> t => GetHostAddressesCore(t.Key, t.Value),
                    _ => null
                }, asyncState);
            }
            else
            {
                return RunAsync(s => s switch
                {
                    string h => GetHostEntryCore(h, AddressFamily.Unspecified),
                    KeyValuePair<string, AddressFamily> t => GetHostEntryCore(t.Key, t.Value),
                    IPAddress a => GetHostEntryCore(a, AddressFamily.Unspecified),
                    KeyValuePair<IPAddress, AddressFamily> t => GetHostEntryCore(t.Key, t.Value),
                    _ => null
                }, asyncState);
            }
        }

        private static Task<T>? GetAddrInfoWithTelemetryAsync<T>(string hostName, bool justAddresses, AddressFamily addressFamily, CancellationToken cancellationToken)
             where T : class
        {
            ValueStopwatch stopwatch = ValueStopwatch.StartNew();
            Task? task = NameResolutionPal.GetAddrInfoAsync(hostName, justAddresses, addressFamily, cancellationToken);

            if (task != null)
            {
                return CompleteAsync(task, hostName, stopwatch);
            }

            // If resolution even did not start don't bother with telemetry.
            // We will retry on thread-pool.
            return null;

            static async Task<T> CompleteAsync(Task task, string hostName, ValueStopwatch stopwatch)
            {
                _  = NameResolutionTelemetry.Log.BeforeResolution(hostName);
                T? result = null;
                try
                {
                    result = await ((Task<T>)task).ConfigureAwait(false);
                    return result;
                }
                finally
                {
                    NameResolutionTelemetry.Log.AfterResolution(stopwatch, successful: result is not null);
                }
            }
        }

        private static Task<TResult> RunAsync<TResult>(Func<object, TResult> func, object arg) =>
            Task.Factory.StartNew(func!, arg, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);