Beispiel #1
0
        internal static PSTraceSource GetTracer(
            string name,
            string description,
            bool traceHeaders)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }
            PSTraceSource psTraceSource1 = (PSTraceSource)null;

            if (PSTraceSource.TraceCatalog.ContainsKey(name))
            {
                psTraceSource1 = PSTraceSource.TraceCatalog[name];
            }
            if (psTraceSource1 == null)
            {
                string str = name;
                if (!PSTraceSource.PreConfiguredTraceSource.ContainsKey(str))
                {
                    if (str.Length > 16)
                    {
                        str = str.Substring(0, 16);
                        if (!PSTraceSource.PreConfiguredTraceSource.ContainsKey(str))
                        {
                            str = (string)null;
                        }
                    }
                    else
                    {
                        str = (string)null;
                    }
                }
                if (str != null)
                {
                    PSTraceSource psTraceSource2 = PSTraceSource.PreConfiguredTraceSource[str];
                    psTraceSource1         = PSTraceSource.GetNewTraceSource(str, description, traceHeaders);
                    psTraceSource1.Options = psTraceSource2.Options;
                    psTraceSource1.Listeners.Clear();
                    psTraceSource1.Listeners.AddRange(psTraceSource2.Listeners);
                    PSTraceSource.TraceCatalog.Add(str, psTraceSource1);
                    PSTraceSource.PreConfiguredTraceSource.Remove(str);
                }
            }
            if (psTraceSource1 == null)
            {
                psTraceSource1 = PSTraceSource.GetNewTraceSource(name, description, traceHeaders);
                PSTraceSource.TraceCatalog[psTraceSource1.FullName] = psTraceSource1;
            }
            if (psTraceSource1.Options != PSTraceSourceOptions.None && traceHeaders)
            {
                psTraceSource1.TraceGlobalAppDomainHeader();
                psTraceSource1.TracerObjectHeader(Assembly.GetCallingAssembly());
            }
            return(psTraceSource1);
        }
Beispiel #2
0
 internal static PSTraceSource GetTracer(string name, string description, bool traceHeaders)
 {
     if (string.IsNullOrEmpty(name))
     {
         throw new ArgumentNullException("name");
     }
     lock (getTracerLock)
     {
         PSTraceSource source = null;
         if (TraceCatalog.ContainsKey(name))
         {
             source = TraceCatalog[name];
         }
         if (source == null)
         {
             string key = name;
             if (!PreConfiguredTraceSource.ContainsKey(key))
             {
                 if (key.Length > 0x10)
                 {
                     key = key.Substring(0, 0x10);
                     if (!PreConfiguredTraceSource.ContainsKey(key))
                     {
                         key = null;
                     }
                 }
                 else
                 {
                     key = null;
                 }
             }
             if (key != null)
             {
                 PSTraceSource source2 = PreConfiguredTraceSource[key];
                 source         = GetNewTraceSource(key, description, traceHeaders);
                 source.Options = source2.Options;
                 source.Listeners.Clear();
                 source.Listeners.AddRange(source2.Listeners);
                 TraceCatalog.Add(key, source);
                 PreConfiguredTraceSource.Remove(key);
             }
         }
         if (source == null)
         {
             source = GetNewTraceSource(name, description, traceHeaders);
             TraceCatalog[source.FullName] = source;
         }
         if ((source.Options != PSTraceSourceOptions.None) && traceHeaders)
         {
             source.TraceGlobalAppDomainHeader();
             source.TracerObjectHeader(Assembly.GetCallingAssembly());
         }
         return(source);
     }
 }
Beispiel #3
0
        /// <summary>
        /// A helper to get an instance of the PSTraceSource class.
        /// </summary>
        /// <param name="name">
        /// The name of the category that this class
        /// will control the tracing for.
        /// </param>
        /// <param name="description">
        /// The description to describe what the category
        /// is used for.
        /// </param>
        /// <param name="traceHeaders">
        /// If true, the line headers will be traced, if false, only the trace message will be traced.
        /// </param>
        /// <returns>
        /// An instance of the PSTraceSource class which is initialized
        /// to trace for the specified category. If multiple callers ask for the same category,
        /// the same PSTraceSource will be returned.
        /// </returns>
        internal static PSTraceSource GetTracer(
            string name,
            string description,
            bool traceHeaders)
        {
            if (string.IsNullOrEmpty(name))
            {
                // 2005/04/13-JonN In theory this should be ArgumentException,
                // but I don't want to deal with loading the string in this
                // low-level code.
                throw new ArgumentNullException(nameof(name));
            }

            lock (PSTraceSource.s_getTracerLock)
            {
                PSTraceSource result = null;

                // See if we can find an PSTraceSource for this category in the catalog.
                PSTraceSource.TraceCatalog.TryGetValue(name, out result);

                // If it's not already in the catalog, see if we can find it in the
                // pre-configured trace source list

                if (result == null)
                {
                    string keyName = name;
                    if (!PSTraceSource.PreConfiguredTraceSource.ContainsKey(keyName))
                    {
                        if (keyName.Length > 16)
                        {
                            keyName = keyName.Substring(0, 16);
                            if (!PSTraceSource.PreConfiguredTraceSource.ContainsKey(keyName))
                            {
                                keyName = null;
                            }
                        }
                        else
                        {
                            keyName = null;
                        }
                    }

                    if (keyName != null)
                    {
                        // Get the pre-configured trace source from the catalog
                        PSTraceSource preconfiguredSource = PSTraceSource.PreConfiguredTraceSource[keyName];

                        result         = PSTraceSource.GetNewTraceSource(keyName, description, traceHeaders);
                        result.Options = preconfiguredSource.Options;
                        result.Listeners.Clear();
                        result.Listeners.AddRange(preconfiguredSource.Listeners);

                        // Add it to the TraceCatalog
                        PSTraceSource.TraceCatalog.Add(keyName, result);

                        // Remove it from the pre-configured catalog
                        PSTraceSource.PreConfiguredTraceSource.Remove(keyName);
                    }
                }

                // Even if there was a PSTraceSource in the catalog, let's replace
                // it with an PSTraceSource to get the added functionality. Anyone using
                // a StructuredTraceSource should be able to do so even with the PSTraceSource
                // instance.

                if (result == null)
                {
                    result = PSTraceSource.GetNewTraceSource(name, description, traceHeaders);
                    PSTraceSource.TraceCatalog[result.FullName] = result;
                }

                if (result.Options != PSTraceSourceOptions.None &&
                    traceHeaders)
                {
                    result.TraceGlobalAppDomainHeader();

                    // Trace the object specific tracer information
                    result.TracerObjectHeader(Assembly.GetCallingAssembly());
                }

                return(result);
            }
        }