<PackageReference Include="System.Configuration.ConfigurationManager" Version="7.0.0-rc.1.22426.10" />

TraceUtils

static class TraceUtils
using System.Collections.Specialized; using System.Configuration; using System.Globalization; using System.IO; using System.Reflection; namespace System.Diagnostics { internal static class TraceUtils { private const string SystemDiagnostics = "System.Diagnostics."; internal static object GetRuntimeObject(string className, Type baseType, string initializeData) { object obj = null; Type type = null; if (className.Length == 0) throw new ConfigurationErrorsException(System.SR.EmptyTypeName_NotAllowed); if (className.StartsWith("System.Diagnostics.")) type = MapToBuiltInTypes(className); if (type == (Type)null) { type = Type.GetType(className); if (type == (Type)null) throw new ConfigurationErrorsException(System.SR.Format(System.SR.Could_not_find_type, className)); } if (!baseType.IsAssignableFrom(type)) throw new ConfigurationErrorsException(System.SR.Format(System.SR.Incorrect_base_type, className, baseType.FullName)); Exception ex = null; try { if (string.IsNullOrEmpty(initializeData)) { if (IsOwnedTL(type)) throw new ConfigurationErrorsException(System.SR.TL_InitializeData_NotSpecified); ConstructorInfo constructor = type.GetConstructor(Array.Empty<Type>()); if (constructor == (ConstructorInfo)null) throw new ConfigurationErrorsException(System.SR.Format(System.SR.Could_not_get_constructor, className)); obj = constructor.Invoke(Array.Empty<object>()); } else { ConstructorInfo constructor2 = type.GetConstructor(new Type[1] { typeof(string) }); if (constructor2 != (ConstructorInfo)null) { if (IsOwnedTextWriterTL(type) && initializeData[0] != Path.DirectorySeparatorChar && initializeData[0] != Path.AltDirectorySeparatorChar && !Path.IsPathRooted(initializeData)) { string configFilePath = System.Diagnostics.DiagnosticsConfiguration.ConfigFilePath; if (!string.IsNullOrEmpty(configFilePath)) { string directoryName = Path.GetDirectoryName(configFilePath); if (directoryName != null) initializeData = Path.Combine(directoryName, initializeData); } } obj = constructor2.Invoke(new object[1] { initializeData }); } else { ConstructorInfo[] constructors = type.GetConstructors(); if (constructors == null) throw new ConfigurationErrorsException(System.SR.Format(System.SR.Could_not_get_constructor, className)); for (int i = 0; i < constructors.Length; i++) { ParameterInfo[] parameters = constructors[i].GetParameters(); if (parameters.Length == 1) { Type parameterType = parameters[0].ParameterType; try { object obj2 = ConvertToBaseTypeOrEnum(initializeData, parameterType); obj = constructors[i].Invoke(new object[1] { obj2 }); } catch (TargetInvocationException ex2) { ex = ex2.InnerException; continue; } catch (Exception ex3) { ex = ex3; continue; } break; } } } } } catch (TargetInvocationException ex4) { ex = ex4.InnerException; } if (obj == null) { if (ex != null) throw new ConfigurationErrorsException(System.SR.Format(System.SR.Could_not_create_type_instance, className), ex); throw new ConfigurationErrorsException(System.SR.Format(System.SR.Could_not_create_type_instance, className)); } return obj; } private static Type MapToBuiltInTypes(string className) { switch (className.Substring("System.Diagnostics.".Length)) { case "DefaultTraceListener": return typeof(DefaultTraceListener); case "SourceFilter": return typeof(SourceFilter); case "EventTypeFilter": return typeof(EventTypeFilter); case "BooleanSwitch": return typeof(BooleanSwitch); case "TraceSwitch": return typeof(TraceSwitch); case "SourceSwitch": return typeof(SourceSwitch); case "ConsoleTraceListener": return typeof(ConsoleTraceListener); case "DelimitedListTraceListener": return typeof(DelimitedListTraceListener); case "XmlWriterTraceListener": return typeof(XmlWriterTraceListener); case "TextWriterTraceListener": return typeof(TextWriterTraceListener); case "EventLogTraceListener": return typeof(EventLogTraceListener); default: return null; } } internal static bool IsOwnedTL(Type type) { if (!(typeof(EventLogTraceListener) == type)) return IsOwnedTextWriterTL(type); return true; } internal static bool IsOwnedTextWriterTL(Type type) { if (!(typeof(XmlWriterTraceListener) == type) && !(typeof(DelimitedListTraceListener) == type)) return typeof(TextWriterTraceListener) == type; return true; } private static object ConvertToBaseTypeOrEnum(string value, Type type) { if (!type.IsEnum) return Convert.ChangeType(value, type, CultureInfo.InvariantCulture); return Enum.Parse(type, value, false); } internal static void CopyStringDictionary(StringDictionary source, StringDictionary dest) { dest.Clear(); foreach (string item in source) { dest[item] = source[item]; } } } }