< Summary

Line coverage
0%
Covered lines: 1
Uncovered lines: 151
Coverable lines: 152
Total lines: 429
Line coverage: 0.6%
Branch coverage
0%
Covered branches: 0
Total branches: 65
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

D:\runner\runtime\src\libraries\Common\src\System\Net\Logging\NetEventSource.Common.Associate.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Diagnostics;
 5using System.Diagnostics.CodeAnalysis;
 6using System.Diagnostics.Tracing;
 7using System.Runtime.CompilerServices;
 8
 9namespace System.Net
 10{
 11    internal sealed partial class NetEventSource
 12    {
 13        private const int AssociateEventId = 3;
 14
 15        /// <summary>Logs a relationship between two objects.</summary>
 16        /// <param name="first">The first object.</param>
 17        /// <param name="second">The second object.</param>
 18        /// <param name="memberName">The calling member.</param>
 19        [NonEvent]
 20        public static void Associate(object first, object second, [CallerMemberName] string? memberName = null) =>
 021            Associate(first, first, second, memberName);
 22
 23        /// <summary>Logs a relationship between two objects.</summary>
 24        /// <param name="thisOrContextObject">`this`, or another object that serves to provide context for the operation
 25        /// <param name="first">The first object.</param>
 26        /// <param name="second">The second object.</param>
 27        /// <param name="memberName">The calling member.</param>
 28        [NonEvent]
 29        public static void Associate(object? thisOrContextObject, object first, object second, [CallerMemberName] string
 030            Log.Associate(IdOf(thisOrContextObject), memberName, IdOf(first), IdOf(second));
 31
 32        [Event(AssociateEventId, Level = EventLevel.Informational, Keywords = Keywords.Default, Message = "[{2}]<-->[{3}
 33        private void Associate(string thisOrContextObject, string? memberName, string first, string second)
 034        {
 035            Debug.Assert(IsEnabled());
 036            WriteEvent(AssociateEventId, thisOrContextObject, memberName ?? MissingMember, first, second);
 037        }
 38
 39        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern",
 40                   Justification = EventSourceSuppressMessage)]
 41        [NonEvent]
 42        private unsafe void WriteEvent(int eventId, string? arg1, string? arg2, string? arg3, string? arg4)
 043        {
 044            arg1 ??= "";
 045            arg2 ??= "";
 046            arg3 ??= "";
 047            arg4 ??= "";
 48
 049            fixed (char* string1Bytes = arg1)
 050            fixed (char* string2Bytes = arg2)
 051            fixed (char* string3Bytes = arg3)
 052            fixed (char* string4Bytes = arg4)
 053            {
 54                const int NumEventDatas = 4;
 055                EventData* descrs = stackalloc EventData[NumEventDatas];
 56
 057                descrs[0] = new EventData
 058                {
 059                    DataPointer = (IntPtr)string1Bytes,
 060                    Size = ((arg1.Length + 1) * 2)
 061                };
 062                descrs[1] = new EventData
 063                {
 064                    DataPointer = (IntPtr)string2Bytes,
 065                    Size = ((arg2.Length + 1) * 2)
 066                };
 067                descrs[2] = new EventData
 068                {
 069                    DataPointer = (IntPtr)string3Bytes,
 070                    Size = ((arg3.Length + 1) * 2)
 071                };
 072                descrs[3] = new EventData
 073                {
 074                    DataPointer = (IntPtr)string4Bytes,
 075                    Size = ((arg4.Length + 1) * 2)
 076                };
 77
 078                WriteEventCore(eventId, NumEventDatas, descrs);
 079            }
 080        }
 81    }
 82}

D:\runner\runtime\src\libraries\Common\src\System\Net\Logging\NetEventSource.Common.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Collections;
 5using System.Diagnostics;
 6using System.Diagnostics.CodeAnalysis;
 7using System.Diagnostics.Tracing;
 8using System.Runtime.CompilerServices;
 9using System.Runtime.InteropServices;
 10
 11#pragma warning disable CA1823 // not all IDs are used by all partial providers
 12
 13namespace System.Net
 14{
 15    // Implementation:
 16    // This partial file is meant to be consumed into each System.Net.* assembly that needs to log.  Each such assembly 
 17    // its own NetEventSource partial class that adds an appropriate [EventSource] attribute, giving it a unique name fo
 18    // Those partials can then also add additional events if needed, starting numbering from the NextAvailableEventId de
 19
 20    // Usage:
 21    // - Operations that may allocate (e.g. boxing a value type, using string interpolation, etc.) or that may have comp
 22    //   at call sites should guard access like:
 23    //       if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(null, $"Found certificate: {cert}"); // info loggin
 24    // - Operations that have zero allocations / measurable computations at call sites can use a simpler pattern, callin
 25    //       NetEventSource.Info(this, "literal string");  // arbitrary message with a literal string
 26    //   Debug.Asserts inside the logging methods will help to flag some misuse if the DEBUG_NETEVENTSOURCE_MISUSE compi
 27    //   However, because it can be difficult by observation to understand all of the costs involved, guarding can be do
 28    // - Messages can be strings, formattable strings, or any other object.  Objects (including those used in formattabl
 29    //   formatting applied, controlled by the Format method.  Partial specializations can also override this formatting
 30    //   method that takes an object and optionally provides a string representation of it, in case a particular library
 31
 32    /// <summary>Provides logging facilities for System.Net libraries.</summary>
 33    internal sealed partial class NetEventSource : EventSource
 34    {
 35        private const string EventSourceSuppressMessage = "Parameters to this method are primitive and are trimmer safe"
 36
 37        /// <summary>The single event source instance to use for all logging.</summary>
 138        public static readonly NetEventSource Log = new NetEventSource();
 39
 40        #region Metadata
 41        public static class Keywords
 42        {
 43            public const EventKeywords Default = (EventKeywords)0x0001;
 44            public const EventKeywords Debug = (EventKeywords)0x0002;
 45        }
 46
 47        private const string MissingMember = "(?)";
 48        private const string NullInstance = "(null)";
 49        private const string StaticMethodObject = "(static)";
 50        private const string NoParameters = "";
 51
 52        private const int InfoEventId = 1;
 53        private const int ErrorEventId = 2;
 54        // private const int AssociateEventId = 3; // Defined in NetEventSource.Common.Associate.cs
 55        // private const int DumpArrayEventId = 4; // Defined in NetEventSource.Common.DumpBuffer.cs
 56
 57        private const int NextAvailableEventId = 5; // Update this value whenever new events are added.  Derived types s
 58        #endregion
 59
 60        #region Info
 61        /// <summary>Logs an information message.</summary>
 62        /// <param name="thisOrContextObject">`this`, or another object that serves to provide context for the operation
 63        /// <param name="formattableString">The message to be logged.</param>
 64        /// <param name="memberName">The calling member.</param>
 65        [NonEvent]
 66        public static void Info(object? thisOrContextObject, FormattableString? formattableString = null, [CallerMemberN
 067            Log.Info(IdOf(thisOrContextObject), memberName, formattableString != null ? Format(formattableString) : NoPa
 68
 69        /// <summary>Logs an information message.</summary>
 70        /// <param name="thisOrContextObject">`this`, or another object that serves to provide context for the operation
 71        /// <param name="message">The message to be logged.</param>
 72        /// <param name="memberName">The calling member.</param>
 73        [NonEvent]
 74        public static void Info(object? thisOrContextObject, object? message, [CallerMemberName] string? memberName = nu
 075            Log.Info(IdOf(thisOrContextObject), memberName, Format(message));
 76
 77        [Event(InfoEventId, Level = EventLevel.Informational, Keywords = Keywords.Default)]
 78        private void Info(string thisOrContextObject, string? memberName, string? message)
 079        {
 080            Debug.Assert(IsEnabled());
 081            WriteEvent(InfoEventId, thisOrContextObject, memberName ?? MissingMember, message);
 082        }
 83        #endregion
 84
 85        #region Error
 86        /// <summary>Logs an error message.</summary>
 87        /// <param name="thisOrContextObject">`this`, or another object that serves to provide context for the operation
 88        /// <param name="formattableString">The message to be logged.</param>
 89        /// <param name="memberName">The calling member.</param>
 90        [NonEvent]
 91        public static void Error(object? thisOrContextObject, FormattableString formattableString, [CallerMemberName] st
 092            Log.ErrorMessage(IdOf(thisOrContextObject), memberName, Format(formattableString));
 93
 94        /// <summary>Logs an error message.</summary>
 95        /// <param name="thisOrContextObject">`this`, or another object that serves to provide context for the operation
 96        /// <param name="message">The message to be logged.</param>
 97        /// <param name="memberName">The calling member.</param>
 98        [NonEvent]
 99        public static void Error(object? thisOrContextObject, object message, [CallerMemberName] string? memberName = nu
 0100            Log.ErrorMessage(IdOf(thisOrContextObject), memberName, Format(message));
 101
 102        [Event(ErrorEventId, Level = EventLevel.Error, Keywords = Keywords.Default)]
 103        private void ErrorMessage(string thisOrContextObject, string? memberName, string? message)
 0104        {
 0105            Debug.Assert(IsEnabled());
 0106            WriteEvent(ErrorEventId, thisOrContextObject, memberName ?? MissingMember, message);
 0107        }
 108        #endregion
 109
 110        #region Helpers
 111        [NonEvent]
 0112        public static string IdOf(object? value) => value != null ? value.GetType().Name + "#" + GetHashCode(value) : Nu
 113
 114        [NonEvent]
 0115        public static int GetHashCode(object? value) => value?.GetHashCode() ?? 0;
 116
 117        [NonEvent]
 118        public static string? Format(object? value)
 0119        {
 120            // If it's null, return a known string for null values
 0121            if (value == null)
 0122            {
 0123                return NullInstance;
 124            }
 125
 126            // Give another partial implementation a chance to provide its own string representation
 0127            string? result = null;
 128            AdditionalCustomizedToString(value, ref result);
 0129            if (result is not null)
 0130            {
 0131                return result;
 132            }
 133
 134            // Format arrays with their element type name and length
 0135            if (value is Array arr)
 0136            {
 0137                return $"{arr.GetType().GetElementType()}[{((Array)value).Length}]";
 138            }
 139
 140            // Format ICollections as the name and count
 0141            if (value is ICollection c)
 0142            {
 0143                return $"{c.GetType().Name}({c.Count})";
 144            }
 145
 146            // Format SafeHandles as their type, hash code, and pointer value
 0147            if (value is SafeHandle handle)
 0148            {
 0149                return $"{handle.GetType().Name}:{handle.GetHashCode()}(0x{handle.DangerousGetHandle():X})";
 150            }
 151
 152            // Format IntPtrs as hex
 0153            if (value is IntPtr)
 0154            {
 0155                return $"0x{value:X}";
 156            }
 157
 158            // If the string representation of the instance would just be its type name,
 159            // use its id instead.
 0160            string? toString = value.ToString();
 0161            if (toString == null || toString == value.GetType().FullName)
 0162            {
 0163                return IdOf(value);
 164            }
 165
 166            // Otherwise, return the original object so that the caller does default formatting.
 0167            return value.ToString();
 0168        }
 169
 170        [NonEvent]
 171        private static string Format(FormattableString s)
 0172        {
 0173            switch (s.ArgumentCount)
 174            {
 0175                case 0: return s.Format;
 0176                case 1: return string.Format(s.Format, Format(s.GetArgument(0)));
 0177                case 2: return string.Format(s.Format, Format(s.GetArgument(0)), Format(s.GetArgument(1)));
 0178                case 3: return string.Format(s.Format, Format(s.GetArgument(0)), Format(s.GetArgument(1)), Format(s.GetA
 179                default:
 0180                    string?[] formattedArgs = new string?[s.ArgumentCount];
 0181                    for (int i = 0; i < formattedArgs.Length; i++)
 0182                    {
 0183                        formattedArgs[i] = Format(s.GetArgument(i));
 0184                    }
 0185                    return string.Format(s.Format, formattedArgs);
 186            }
 0187        }
 188
 189        static partial void AdditionalCustomizedToString(object value, ref string? result);
 190        #endregion
 191
 192        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern",
 193                   Justification = EventSourceSuppressMessage)]
 194        [NonEvent]
 195        private unsafe void WriteEvent(int eventId, string? arg1, string? arg2, int arg3)
 196        {
 197            arg1 ??= "";
 198            arg2 ??= "";
 199
 200            fixed (char* arg1Ptr = arg1)
 201            fixed (char* arg2Ptr = arg2)
 202            {
 203                const int NumEventDatas = 3;
 204                EventData* descrs = stackalloc EventData[NumEventDatas];
 205
 206                descrs[0] = new EventData
 207                {
 208                    DataPointer = (IntPtr)(arg1Ptr),
 209                    Size = (arg1.Length + 1) * sizeof(char)
 210                };
 211                descrs[1] = new EventData
 212                {
 213                    DataPointer = (IntPtr)(arg2Ptr),
 214                    Size = (arg2.Length + 1) * sizeof(char)
 215                };
 216                descrs[2] = new EventData
 217                {
 218                    DataPointer = (IntPtr)(&arg3),
 219                    Size = sizeof(int)
 220                };
 221
 222                WriteEventCore(eventId, NumEventDatas, descrs);
 223            }
 224        }
 225    }
 226}

D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\NetEventSource.Http.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Diagnostics;
 5using System.Diagnostics.CodeAnalysis;
 6using System.Diagnostics.Tracing;
 7
 8namespace System.Net
 9{
 10    [EventSource(Name = "Private.InternalDiagnostics.System.Net.Http")]
 11    internal sealed partial class NetEventSource
 12    {
 13        private const int UriBaseAddressId = NextAvailableEventId;
 14        private const int ContentNullId = UriBaseAddressId + 1;
 15        private const int HeadersInvalidValueId = ContentNullId + 1;
 16        private const int HandlerMessageId = HeadersInvalidValueId + 1;
 17        private const int AuthenticationInfoId = HandlerMessageId + 1;
 18        private const int AuthenticationErrorId = AuthenticationInfoId + 1;
 19        private const int HandlerErrorId = AuthenticationErrorId + 1;
 20
 21        [NonEvent]
 22        public static void UriBaseAddress(object obj, Uri? baseAddress)
 023        {
 024            Debug.Assert(Log.IsEnabled());
 025            Log.UriBaseAddress(baseAddress?.ToString(), IdOf(obj));
 026        }
 27
 28        [Event(UriBaseAddressId, Keywords = Keywords.Debug, Level = EventLevel.Informational)]
 29        private void UriBaseAddress(string? uriBaseAddress, string objName) =>
 030            WriteEvent(UriBaseAddressId, uriBaseAddress, objName);
 31
 32        [NonEvent]
 33        public static void ContentNull(object obj)
 034        {
 035            Debug.Assert(Log.IsEnabled());
 036            Log.ContentNull(IdOf(obj), GetHashCode(obj));
 037        }
 38
 39        [Event(ContentNullId, Keywords = Keywords.Debug, Level = EventLevel.Informational)]
 40        private void ContentNull(string objName, int objHash) =>
 041            WriteEvent(ContentNullId, objName, objHash);
 42
 43        [Event(HeadersInvalidValueId, Keywords = Keywords.Debug, Level = EventLevel.Error)]
 44        public void HeadersInvalidValue(string name, string rawValue) =>
 045            WriteEvent(HeadersInvalidValueId, name, rawValue);
 46
 47        [Event(HandlerMessageId, Keywords = Keywords.Debug, Level = EventLevel.Verbose)]
 48        public void HandlerMessage(int poolId, int workerId, int requestId, string? memberName, string? message) =>
 049            WriteEvent(HandlerMessageId, poolId, workerId, requestId, memberName, message);
 50
 51        [Event(HandlerErrorId, Keywords = Keywords.Debug, Level = EventLevel.Error)]
 52        public void HandlerMessageError(int poolId, int workerId, int requestId, string? memberName, string message) =>
 053            WriteEvent(HandlerErrorId, poolId, workerId, requestId, memberName, message);
 54
 55        [NonEvent]
 56        public static void AuthenticationInfo(Uri uri, string message)
 057        {
 058            Debug.Assert(Log.IsEnabled());
 059            Log.AuthenticationInfo(uri?.ToString(), message);
 060        }
 61
 62        [Event(AuthenticationInfoId, Keywords = Keywords.Debug, Level = EventLevel.Verbose)]
 63        public void AuthenticationInfo(string? uri, string message) =>
 064            WriteEvent(AuthenticationInfoId, uri, message);
 65
 66        [NonEvent]
 67        public static void AuthenticationError(Uri? uri, string message)
 068        {
 069            Debug.Assert(Log.IsEnabled());
 070            Log.AuthenticationError(uri?.ToString(), message);
 071        }
 72
 73        [Event(AuthenticationErrorId, Keywords = Keywords.Debug, Level = EventLevel.Error)]
 74        public void AuthenticationError(string? uri, string message) =>
 075            WriteEvent(AuthenticationErrorId, uri, message);
 76
 77        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern",
 78                   Justification = EventSourceSuppressMessage)]
 79        [NonEvent]
 80        private unsafe void WriteEvent(int eventId, int arg1, int arg2, int arg3, string? arg4, string? arg5)
 081        {
 082            arg4 ??= "";
 083            arg5 ??= "";
 84
 085            fixed (char* string4Bytes = arg4)
 086            fixed (char* string5Bytes = arg5)
 087            {
 88                const int NumEventDatas = 5;
 089                EventData* descrs = stackalloc EventData[NumEventDatas];
 90
 091                descrs[0] = new EventData
 092                {
 093                    DataPointer = (IntPtr)(&arg1),
 094                    Size = sizeof(int)
 095                };
 096                descrs[1] = new EventData
 097                {
 098                    DataPointer = (IntPtr)(&arg2),
 099                    Size = sizeof(int)
 0100                };
 0101                descrs[2] = new EventData
 0102                {
 0103                    DataPointer = (IntPtr)(&arg3),
 0104                    Size = sizeof(int)
 0105                };
 0106                descrs[3] = new EventData
 0107                {
 0108                    DataPointer = (IntPtr)string4Bytes,
 0109                    Size = ((arg4.Length + 1) * 2)
 0110                };
 0111                descrs[4] = new EventData
 0112                {
 0113                    DataPointer = (IntPtr)string5Bytes,
 0114                    Size = ((arg5.Length + 1) * 2)
 0115                };
 116
 0117                WriteEventCore(eventId, NumEventDatas, descrs);
 0118            }
 0119        }
 120    }
 121}