| | | 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 | | |
| | | 4 | | using System.Collections; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.Diagnostics.Tracing; |
| | | 8 | | using System.Runtime.CompilerServices; |
| | | 9 | | using System.Runtime.InteropServices; |
| | | 10 | | |
| | | 11 | | #pragma warning disable CA1823 // not all IDs are used by all partial providers |
| | | 12 | | |
| | | 13 | | namespace 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> |
| | 1 | 38 | | 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 |
| | 0 | 67 | | 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 |
| | 0 | 75 | | 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) |
| | 0 | 79 | | { |
| | 0 | 80 | | Debug.Assert(IsEnabled()); |
| | 0 | 81 | | WriteEvent(InfoEventId, thisOrContextObject, memberName ?? MissingMember, message); |
| | 0 | 82 | | } |
| | | 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 |
| | 0 | 92 | | 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 |
| | 0 | 100 | | 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) |
| | 0 | 104 | | { |
| | 0 | 105 | | Debug.Assert(IsEnabled()); |
| | 0 | 106 | | WriteEvent(ErrorEventId, thisOrContextObject, memberName ?? MissingMember, message); |
| | 0 | 107 | | } |
| | | 108 | | #endregion |
| | | 109 | | |
| | | 110 | | #region Helpers |
| | | 111 | | [NonEvent] |
| | 0 | 112 | | public static string IdOf(object? value) => value != null ? value.GetType().Name + "#" + GetHashCode(value) : Nu |
| | | 113 | | |
| | | 114 | | [NonEvent] |
| | 0 | 115 | | public static int GetHashCode(object? value) => value?.GetHashCode() ?? 0; |
| | | 116 | | |
| | | 117 | | [NonEvent] |
| | | 118 | | public static string? Format(object? value) |
| | 0 | 119 | | { |
| | | 120 | | // If it's null, return a known string for null values |
| | 0 | 121 | | if (value == null) |
| | 0 | 122 | | { |
| | 0 | 123 | | return NullInstance; |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | // Give another partial implementation a chance to provide its own string representation |
| | 0 | 127 | | string? result = null; |
| | | 128 | | AdditionalCustomizedToString(value, ref result); |
| | 0 | 129 | | if (result is not null) |
| | 0 | 130 | | { |
| | 0 | 131 | | return result; |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | // Format arrays with their element type name and length |
| | 0 | 135 | | if (value is Array arr) |
| | 0 | 136 | | { |
| | 0 | 137 | | return $"{arr.GetType().GetElementType()}[{((Array)value).Length}]"; |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | // Format ICollections as the name and count |
| | 0 | 141 | | if (value is ICollection c) |
| | 0 | 142 | | { |
| | 0 | 143 | | return $"{c.GetType().Name}({c.Count})"; |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | // Format SafeHandles as their type, hash code, and pointer value |
| | 0 | 147 | | if (value is SafeHandle handle) |
| | 0 | 148 | | { |
| | 0 | 149 | | return $"{handle.GetType().Name}:{handle.GetHashCode()}(0x{handle.DangerousGetHandle():X})"; |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | // Format IntPtrs as hex |
| | 0 | 153 | | if (value is IntPtr) |
| | 0 | 154 | | { |
| | 0 | 155 | | 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. |
| | 0 | 160 | | string? toString = value.ToString(); |
| | 0 | 161 | | if (toString == null || toString == value.GetType().FullName) |
| | 0 | 162 | | { |
| | 0 | 163 | | return IdOf(value); |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | // Otherwise, return the original object so that the caller does default formatting. |
| | 0 | 167 | | return value.ToString(); |
| | 0 | 168 | | } |
| | | 169 | | |
| | | 170 | | [NonEvent] |
| | | 171 | | private static string Format(FormattableString s) |
| | 0 | 172 | | { |
| | 0 | 173 | | switch (s.ArgumentCount) |
| | | 174 | | { |
| | 0 | 175 | | case 0: return s.Format; |
| | 0 | 176 | | case 1: return string.Format(s.Format, Format(s.GetArgument(0))); |
| | 0 | 177 | | case 2: return string.Format(s.Format, Format(s.GetArgument(0)), Format(s.GetArgument(1))); |
| | 0 | 178 | | case 3: return string.Format(s.Format, Format(s.GetArgument(0)), Format(s.GetArgument(1)), Format(s.GetA |
| | | 179 | | default: |
| | 0 | 180 | | string?[] formattedArgs = new string?[s.ArgumentCount]; |
| | 0 | 181 | | for (int i = 0; i < formattedArgs.Length; i++) |
| | 0 | 182 | | { |
| | 0 | 183 | | formattedArgs[i] = Format(s.GetArgument(i)); |
| | 0 | 184 | | } |
| | 0 | 185 | | return string.Format(s.Format, formattedArgs); |
| | | 186 | | } |
| | 0 | 187 | | } |
| | | 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 | | } |