Fix IDE0025 (use expression body for properties)
[mono-project.git] / netcore / System.Private.CoreLib / shared / System / Diagnostics / Tracing / TraceLogging / TraceLoggingEventTypes.cs
blobf003fa0d0afc8c3a2c99c72a7d71eaccda5dbe08
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 // See the LICENSE file in the project root for more information.
5 using System;
6 using System.Collections.Generic;
7 using Interlocked = System.Threading.Interlocked;
9 #if !ES_BUILD_AGAINST_DOTNET_V35
10 using Contract = System.Diagnostics.Contracts.Contract;
11 #else
12 using Contract = Microsoft.Diagnostics.Contracts.Internal.Contract;
13 #endif
15 #if ES_BUILD_STANDALONE
16 namespace Microsoft.Diagnostics.Tracing
17 #else
18 namespace System.Diagnostics.Tracing
19 #endif
21 /// <summary>
22 /// TraceLogging: Used when calling EventSource.WriteMultiMerge.
23 /// Stores the type information to use when writing the event fields.
24 /// </summary>
25 public class TraceLoggingEventTypes
27 internal readonly TraceLoggingTypeInfo[] typeInfos;
28 #if FEATURE_PERFTRACING
29 internal readonly string[]? paramNames;
30 #endif
31 internal readonly string name;
32 internal readonly EventTags tags;
33 internal readonly byte level;
34 internal readonly byte opcode;
35 internal readonly EventKeywords keywords;
36 internal readonly byte[] typeMetadata;
37 internal readonly int scratchSize;
38 internal readonly int dataCount;
39 internal readonly int pinCount;
40 private ConcurrentSet<KeyValuePair<string, EventTags>, NameInfo> nameInfos;
42 /// <summary>
43 /// Initializes a new instance of TraceLoggingEventTypes corresponding
44 /// to the name, flags, and types provided. Always uses the default
45 /// TypeInfo for each Type.
46 /// </summary>
47 /// <param name="name">
48 /// The name to use when the name parameter passed to
49 /// EventSource.Write is null. This value must not be null.
50 /// </param>
51 /// <param name="tags">
52 /// Tags to add to the event if the tags are not set via options.
53 /// </param>
54 /// <param name="types">
55 /// The types of the fields in the event. This value must not be null.
56 /// </param>
57 internal TraceLoggingEventTypes(
58 string name,
59 EventTags tags,
60 params Type[] types)
61 : this(tags, name, MakeArray(types))
65 /// <summary>
66 /// Returns a new instance of TraceLoggingEventInfo corresponding to the name,
67 /// flags, and typeInfos provided.
68 /// </summary>
69 /// <param name="name">
70 /// The name to use when the name parameter passed to
71 /// EventSource.Write is null. This value must not be null.
72 /// </param>
73 /// <param name="tags">
74 /// Tags to add to the event if the tags are not set via options.
75 /// </param>
76 /// <param name="typeInfos">
77 /// The types of the fields in the event. This value must not be null.
78 /// </param>
79 /// <returns>
80 /// An instance of TraceLoggingEventInfo with DefaultName set to the specified name
81 /// and with the specified typeInfos.
82 /// </returns>
83 internal TraceLoggingEventTypes(
84 string name,
85 EventTags tags,
86 params TraceLoggingTypeInfo[] typeInfos)
87 : this(tags, name, MakeArray(typeInfos))
91 internal TraceLoggingEventTypes(
92 string name,
93 EventTags tags,
94 System.Reflection.ParameterInfo[] paramInfos)
96 if (name == null)
98 throw new ArgumentNullException(nameof(name));
101 this.typeInfos = MakeArray(paramInfos);
102 #if FEATURE_PERFTRACING
103 this.paramNames = MakeParamNameArray(paramInfos);
104 #endif
105 this.name = name;
106 this.tags = tags;
107 this.level = Statics.DefaultLevel;
109 var collector = new TraceLoggingMetadataCollector();
110 for (int i = 0; i < typeInfos.Length; ++i)
112 TraceLoggingTypeInfo typeInfo = typeInfos[i];
113 this.level = Statics.Combine((int)typeInfo.Level, this.level);
114 this.opcode = Statics.Combine((int)typeInfo.Opcode, this.opcode);
115 this.keywords |= typeInfo.Keywords;
116 string? paramName = paramInfos[i].Name;
117 if (Statics.ShouldOverrideFieldName(paramName!))
119 paramName = typeInfo.Name;
121 typeInfo.WriteMetadata(collector, paramName, EventFieldFormat.Default);
124 this.typeMetadata = collector.GetMetadata();
125 this.scratchSize = collector.ScratchSize;
126 this.dataCount = collector.DataCount;
127 this.pinCount = collector.PinCount;
130 private TraceLoggingEventTypes(
131 EventTags tags,
132 string defaultName,
133 TraceLoggingTypeInfo[] typeInfos)
135 if (defaultName == null)
137 throw new ArgumentNullException(nameof(defaultName));
140 this.typeInfos = typeInfos;
141 this.name = defaultName;
142 this.tags = tags;
143 this.level = Statics.DefaultLevel;
145 var collector = new TraceLoggingMetadataCollector();
146 foreach (TraceLoggingTypeInfo typeInfo in typeInfos)
148 this.level = Statics.Combine((int)typeInfo.Level, this.level);
149 this.opcode = Statics.Combine((int)typeInfo.Opcode, this.opcode);
150 this.keywords |= typeInfo.Keywords;
151 typeInfo.WriteMetadata(collector, null, EventFieldFormat.Default);
154 this.typeMetadata = collector.GetMetadata();
155 this.scratchSize = collector.ScratchSize;
156 this.dataCount = collector.DataCount;
157 this.pinCount = collector.PinCount;
160 /// <summary>
161 /// Gets the default name that will be used for events with this descriptor.
162 /// </summary>
163 internal string Name => this.name;
165 /// <summary>
166 /// Gets the default level that will be used for events with this descriptor.
167 /// </summary>
168 internal EventLevel Level => (EventLevel)this.level;
170 /// <summary>
171 /// Gets the default opcode that will be used for events with this descriptor.
172 /// </summary>
173 internal EventOpcode Opcode => (EventOpcode)this.opcode;
175 /// <summary>
176 /// Gets the default set of keywords that will added to events with this descriptor.
177 /// </summary>
178 internal EventKeywords Keywords => (EventKeywords)this.keywords;
180 /// <summary>
181 /// Gets the default tags that will be added events with this descriptor.
182 /// </summary>
183 internal EventTags Tags => this.tags;
185 internal NameInfo GetNameInfo(string name, EventTags tags)
187 NameInfo? ret = this.nameInfos.TryGet(new KeyValuePair<string, EventTags>(name, tags));
188 if (ret == null)
190 ret = this.nameInfos.GetOrAdd(new NameInfo(name, tags, this.typeMetadata.Length));
193 return ret;
196 private TraceLoggingTypeInfo[] MakeArray(System.Reflection.ParameterInfo[] paramInfos)
198 if (paramInfos == null)
200 throw new ArgumentNullException(nameof(paramInfos));
203 var recursionCheck = new List<Type>(paramInfos.Length);
204 var result = new TraceLoggingTypeInfo[paramInfos.Length];
205 for (int i = 0; i < paramInfos.Length; ++i)
207 result[i] = TraceLoggingTypeInfo.GetInstance(paramInfos[i].ParameterType, recursionCheck);
210 return result;
213 private static TraceLoggingTypeInfo[] MakeArray(Type[] types)
215 if (types == null)
217 throw new ArgumentNullException(nameof(types));
220 var recursionCheck = new List<Type>(types.Length);
221 var result = new TraceLoggingTypeInfo[types.Length];
222 for (int i = 0; i < types.Length; i++)
224 result[i] = TraceLoggingTypeInfo.GetInstance(types[i], recursionCheck);
227 return result;
230 private static TraceLoggingTypeInfo[] MakeArray(
231 TraceLoggingTypeInfo[] typeInfos)
233 if (typeInfos == null)
235 throw new ArgumentNullException(nameof(typeInfos));
238 return (TraceLoggingTypeInfo[])typeInfos.Clone(); ;
241 #if FEATURE_PERFTRACING
242 private static string[] MakeParamNameArray(
243 System.Reflection.ParameterInfo[] paramInfos)
245 string[] paramNames = new string[paramInfos.Length];
246 for (int i = 0; i < paramNames.Length; i++)
248 paramNames[i] = paramInfos[i].Name!;
251 return paramNames;
253 #endif