Пример #1
0
 internal PSTraceSource(string fullName, string name, string description, bool traceHeaders)
 {
     if (string.IsNullOrEmpty(fullName))
     {
         throw new ArgumentNullException("fullName");
     }
     try
     {
         this.fullName = fullName;
         this.name = name;
         if (string.Equals(Environment.GetEnvironmentVariable("MshEnableTrace"), "True", StringComparison.OrdinalIgnoreCase))
         {
             string str2 = this.TraceSource.Attributes["Options"];
             if (str2 != null)
             {
                 this.flags = (PSTraceSourceOptions) Enum.Parse(typeof(PSTraceSourceOptions), str2, true);
             }
         }
         this.showHeaders = traceHeaders;
         this.description = description;
     }
     catch (XmlException)
     {
         this.flags = PSTraceSourceOptions.None;
     }
     catch (ConfigurationException)
     {
         this.flags = PSTraceSourceOptions.None;
     }
 }
Пример #2
0
        private static StringBuilder GetLinePrefix(PSTraceSourceOptions flag)
        {
            StringBuilder builder = new StringBuilder();

            builder.AppendFormat(Thread.CurrentThread.CurrentCulture, " {0,-11} ", new object[] { Enum.GetName(typeof(PSTraceSourceOptions), flag) });
            return(builder);
        }
Пример #3
0
 internal PSTraceSource(string fullName, string name, string description, bool traceHeaders)
 {
     if (string.IsNullOrEmpty(fullName))
     {
         throw new ArgumentNullException("fullName");
     }
     try
     {
         this.fullName = fullName;
         this.name     = name;
         if (string.Equals(Environment.GetEnvironmentVariable("MshEnableTrace"), "True", StringComparison.OrdinalIgnoreCase))
         {
             string str2 = this.TraceSource.Attributes["Options"];
             if (str2 != null)
             {
                 this.flags = (PSTraceSourceOptions)Enum.Parse(typeof(PSTraceSourceOptions), str2, true);
             }
         }
         this.showHeaders = traceHeaders;
         this.description = description;
     }
     catch (XmlException)
     {
         this.flags = PSTraceSourceOptions.None;
     }
     catch (ConfigurationException)
     {
         this.flags = PSTraceSourceOptions.None;
     }
 }
Пример #4
0
        private static StringBuilder GetLinePrefix(PSTraceSourceOptions flag)
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.AppendFormat((IFormatProvider)Thread.CurrentThread.CurrentCulture, " {0,-11} ", (object)Enum.GetName(typeof(PSTraceSourceOptions), (object)flag));
            return(stringBuilder);
        }
Пример #5
0
 internal ScopeTracer(
     PSTraceSource tracer,
     PSTraceSourceOptions flag,
     string scopeOutputFormatter,
     string leavingScopeFormatter,
     string scopeName)
 {
     this._tracer = tracer;
     this.ScopeTracerHelper(flag, scopeOutputFormatter, leavingScopeFormatter, scopeName, "");
 }
Пример #6
0
 internal ScopeTracer(PSTraceSource tracer, PSTraceSourceOptions flag, string scopeOutputFormatter, string leavingScopeFormatter, string scopeName, string format, params object[] args)
 {
     this._tracer = tracer;
     if (format != null)
     {
         this.ScopeTracerHelper(flag, scopeOutputFormatter, leavingScopeFormatter, scopeName, format, args);
     }
     else
     {
         this.ScopeTracerHelper(flag, scopeOutputFormatter, leavingScopeFormatter, scopeName, "", new object[0]);
     }
 }
Пример #7
0
 internal ScopeTracer(PSTraceSource tracer, PSTraceSourceOptions flag, string scopeOutputFormatter, string leavingScopeFormatter, string scopeName, string format, params object[] args)
 {
     this._tracer = tracer;
     if (format != null)
     {
         this.ScopeTracerHelper(flag, scopeOutputFormatter, leavingScopeFormatter, scopeName, format, args);
     }
     else
     {
         this.ScopeTracerHelper(flag, scopeOutputFormatter, leavingScopeFormatter, scopeName, "", new object[0]);
     }
 }
Пример #8
0
 internal void ScopeTracerHelper(PSTraceSourceOptions flag, string scopeOutputFormatter, string leavingScopeFormatter, string scopeName, string format, params object[] args)
 {
     this._flag = flag;
     this._scopeName = scopeName;
     this._leavingScopeFormatter = leavingScopeFormatter;
     StringBuilder builder = new StringBuilder();
     if (!string.IsNullOrEmpty(scopeOutputFormatter))
     {
         builder.AppendFormat(Thread.CurrentThread.CurrentCulture, scopeOutputFormatter, new object[] { this._scopeName });
     }
     if (!string.IsNullOrEmpty(format))
     {
         builder.AppendFormat(Thread.CurrentThread.CurrentCulture, format, args);
     }
     this._tracer.OutputLine(this._flag, builder.ToString(), new object[0]);
     PSTraceSource.ThreadIndentLevel++;
 }
Пример #9
0
        internal void ScopeTracerHelper(PSTraceSourceOptions flag, string scopeOutputFormatter, string leavingScopeFormatter, string scopeName, string format, params object[] args)
        {
            this._flag                  = flag;
            this._scopeName             = scopeName;
            this._leavingScopeFormatter = leavingScopeFormatter;
            StringBuilder builder = new StringBuilder();

            if (!string.IsNullOrEmpty(scopeOutputFormatter))
            {
                builder.AppendFormat(Thread.CurrentThread.CurrentCulture, scopeOutputFormatter, new object[] { this._scopeName });
            }
            if (!string.IsNullOrEmpty(format))
            {
                builder.AppendFormat(Thread.CurrentThread.CurrentCulture, format, args);
            }
            this._tracer.OutputLine(this._flag, builder.ToString(), new object[0]);
            PSTraceSource.ThreadIndentLevel++;
        }
Пример #10
0
 private void FormatOutputLine(PSTraceSourceOptions flag, string classFormatter, string format, params object[] args)
 {
     try
     {
         StringBuilder builder = new StringBuilder();
         if (classFormatter != null)
         {
             builder.Append(classFormatter);
         }
         if (format != null)
         {
             builder.AppendFormat(Thread.CurrentThread.CurrentCulture, format, args);
         }
         this.OutputLine(flag, builder.ToString(), new object[0]);
     }
     catch
     {
     }
 }
Пример #11
0
 internal void OutputLine(PSTraceSourceOptions flag, string format, params object[] args)
 {
     if (this.alreadyTracing)
     {
         return;
     }
     this.alreadyTracing = true;
     try
     {
         StringBuilder lineBuilder = new StringBuilder();
         if (this.showHeaders)
         {
             lineBuilder.Append((object)PSTraceSource.GetLinePrefix(flag));
         }
         PSTraceSource.AddTab(ref lineBuilder);
         if (args != null && args.Length > 0)
         {
             for (int index = 0; index < args.Length; ++index)
             {
                 if (args[index] == null)
                 {
                     args[index] = (object)"null";
                 }
             }
             lineBuilder.AppendFormat((IFormatProvider)Thread.CurrentThread.CurrentCulture, format, args);
         }
         else
         {
             lineBuilder.Append(format);
         }
         this.TraceSource.TraceInformation(lineBuilder.ToString());
     }
     finally
     {
         this.alreadyTracing = false;
     }
 }
Пример #12
0
 internal void OutputLine(PSTraceSourceOptions flag, string format, params object[] args)
 {
     if (!this.alreadyTracing)
     {
         this.alreadyTracing = true;
         try
         {
             StringBuilder lineBuilder = new StringBuilder();
             if (this.showHeaders)
             {
                 lineBuilder.Append(GetLinePrefix(flag));
             }
             AddTab(ref lineBuilder);
             if ((args != null) && (args.Length > 0))
             {
                 for (int i = 0; i < args.Length; i++)
                 {
                     if (args[i] == null)
                     {
                         args[i] = "null";
                     }
                 }
                 lineBuilder.AppendFormat(Thread.CurrentThread.CurrentCulture, format, args);
             }
             else
             {
                 lineBuilder.Append(format);
             }
             this.TraceSource.TraceInformation(lineBuilder.ToString());
         }
         finally
         {
             this.alreadyTracing = false;
         }
     }
 }
Пример #13
0
        /// <summary>
        /// Helper for the ScopeTracer constructor
        /// </summary>
        /// 
        /// <param name="flag">
        /// The flag that caused this line of tracing to be traced.
        /// </param>
        /// 
        /// <param name="scopeOutputFormatter">
        /// This format string is used to determine the
        /// general output format for the scope. For instance,
        /// TraceMethod would probably provide a formatter similar
        /// to "Entering: {0}: {1}" where {0} is the name of the
        /// method and {1} is the additional formatted info provided.
        /// </param>
        /// 
        /// <param name="leavingScopeFormatter">
        /// The format string used to determine the general output
        /// format for the scope when the Dispose method is called.
        /// </param>
        /// 
        /// <param name="scopeName">
        /// The name of the scope being entered
        /// </param>
        /// 
        /// <param name="format">
        /// The format of any additional arguments which will be appended
        /// to the "Entering" line of trace output
        /// </param>
        /// 
        /// <param name="args">
        /// Arguments to the format string.
        /// </param>
        /// 
        internal void ScopeTracerHelper(
            PSTraceSourceOptions flag,
            string scopeOutputFormatter,
            string leavingScopeFormatter,
            string scopeName,
            string format,
            params object[] args)
        {
            // Store the flags, scopeName, and the leavingScopeFormatter
            // so that it can be used in the Dispose method

            _flag = flag;
            _scopeName = scopeName;
            _leavingScopeFormatter = leavingScopeFormatter;

            // Format the string for output

            StringBuilder output = new StringBuilder();

            if (!String.IsNullOrEmpty(scopeOutputFormatter))
            {
                output.AppendFormat(
                    CultureInfo.CurrentCulture,
                    scopeOutputFormatter,
                    _scopeName);
            }

            if (!String.IsNullOrEmpty(format))
            {
                output.AppendFormat(
                    CultureInfo.CurrentCulture,
                    format,
                    args);
            }

            // Now write the trace

            _tracer.OutputLine(_flag, output.ToString());

            // Increment the current thread indent level

            PSTraceSource.ThreadIndentLevel++;
        }
Пример #14
0
        /// <summary>
        /// Constructor that traces the scope name
        /// and raises the indent level in thread
        /// local storage.
        /// </summary>
        /// 
        /// <param name="tracer">
        /// The trace object that is to be used for output
        /// </param>
        /// 
        /// <param name="flag">
        /// The PSTraceSourceOptions that is causing the scope object to 
        /// be created.
        /// </param>
        /// 
        /// <param name="scopeOutputFormatter">
        /// This format string is used to determine the
        /// general output format for the scope. For instance,
        /// TraceMethod would probably provide a formatter similar
        /// to "Entering: {0}: {1}" where {0} is the name of the
        /// method and {1} is the additional formatted info provided.
        /// </param>
        /// 
        /// <param name="leavingScopeFormatter">
        /// The format string used to determine the general output
        /// format for the scope when the Dispose method is called.
        /// </param>
        /// 
        /// <param name="scopeName">
        /// The name of the scope that is being traced
        /// </param>
        /// 
        /// <param name="format">
        /// The format of any additional arguments which will be appended
        /// to the line of trace output
        /// </param>
        /// 
        /// <param name="args">
        /// Arguments to the format string.
        /// </param>
        /// 
        internal ScopeTracer(
            PSTraceSource tracer,
            PSTraceSourceOptions flag,
            string scopeOutputFormatter,
            string leavingScopeFormatter,
            string scopeName,
            string format,
            params object[] args)
        {
            _tracer = tracer;

            // Call the helper

            if (format != null)
            {
                ScopeTracerHelper(
                    flag,
                    scopeOutputFormatter,
                    leavingScopeFormatter,
                    scopeName,
                    format,
                    args);
            }
            else
            {
                ScopeTracerHelper(
                    flag,
                    scopeOutputFormatter,
                    leavingScopeFormatter,
                    scopeName,
                    "");
            }
        }
Пример #15
0
        /// <summary>
        /// Constructor that traces the scope name
        /// and raises the indent level in thread
        /// local storage.
        /// </summary>
        /// 
        /// <param name="tracer">
        /// The trace object that is to be used for output
        /// </param>
        /// 
        /// <param name="flag">
        /// The PSTraceSourceOptions that is causing the scope object to 
        /// be created.
        /// </param>
        /// 
        /// <param name="scopeOutputFormatter">
        /// This format string is used to determine the
        /// general output format for the scope. For instance,
        /// TraceMethod would probably provide a formatter similar
        /// to "Entering: {0}: {1}" where {0} is the name of the
        /// method and {1} is the additional formatted info provided.
        /// </param>
        /// 
        /// <param name="leavingScopeFormatter">
        /// The format string used to determine the general output
        /// format for the scope when the Dispose method is called.
        /// </param>
        /// 
        /// <param name="scopeName">
        /// The name of the scope that is being traced
        /// </param>
        /// 
        internal ScopeTracer(
            PSTraceSource tracer,
            PSTraceSourceOptions flag,
            string scopeOutputFormatter,
            string leavingScopeFormatter,
            string scopeName)
        {
            _tracer = tracer;

            // Call the helper

            ScopeTracerHelper(
                flag,
                scopeOutputFormatter,
                leavingScopeFormatter,
                scopeName,
                "");
        }
Пример #16
0
        /// <summary>
        /// Composes a line of trace output and then writes it
        /// </summary>
        /// 
        /// <param name="flag">
        /// The flag that caused the line to be traced
        /// </param>
        /// 
        /// <param name="format">
        /// The string to write with format symbols if necessary
        /// </param>
        /// 
        /// <param name="args">
        /// Arguments to the format string
        /// </param>
        /// 
        /// <remarks>
        /// The line is composed by prefixing the process name, thread ID,
        /// and tick count. Then the indenting is added. Then the
        /// specified string is formatted. Finally the finished string
        /// is output using the Trace class.
        /// </remarks>
        internal void OutputLine(
            PSTraceSourceOptions flag,
            string format,
            params object[] args)
        {
            // if already tracing something for this current TraceSource,
            // dont trace again. This will block cyclic-loops from happening.
            if (_alreadyTracing)
            {
                return;
            }

            _alreadyTracing = true;
            try
            {
                Diagnostics.Assert(
                    format != null,
                    "The format string should not be null");

                StringBuilder lineBuilder = new StringBuilder();

                if (ShowHeaders)
                {
                    // Get the line prefix string which includes things
                    // like App name, clock tick, thread ID, etc.

                    lineBuilder.Append(GetLinePrefix(flag));
                }

                // Add the spaces for the indent

                AddTab(ref lineBuilder);

                if (args != null && args.Length > 0)
                {
                    for (int index = 0; index < args.Length; ++index)
                    {
                        if (args[index] == null)
                        {
                            args[index] = "null";
                        }
                    }

                    lineBuilder.AppendFormat(
                        CultureInfo.CurrentCulture,
                        format,
                        args);
                }
                else
                {
                    lineBuilder.Append(format);
                }

                this.TraceSource.TraceInformation(lineBuilder.ToString());
            }
            finally
            {
                // reset tracing for the current trace source..
                // so future traces can go through.
                _alreadyTracing = false;
            }
        }
Пример #17
0
        private static StringBuilder GetLinePrefix(PSTraceSourceOptions flag)
        {
            StringBuilder prefixBuilder = new StringBuilder();

            // Add the flag that caused this line to be traced

            prefixBuilder.AppendFormat(
                CultureInfo.CurrentCulture,
                " {0,-11} ",
                Enum.GetName(typeof(PSTraceSourceOptions), flag));
            return prefixBuilder;
        }
Пример #18
0
        /// <summary>
        /// Formats the specified text and then traces it
        /// </summary>
        /// 
        /// <param name="flag">
        /// The flag that met the criteria to have this line traced.
        /// </param>
        /// 
        /// <param name="classFormatter">
        /// This is the trace class formatter. For instance,
        /// TraceError has a formatter like "ERROR: {0}"
        /// </param>
        /// 
        /// <param name="format">
        /// Additional format string
        /// </param>
        /// 
        /// <param name="args">
        /// Arguments for the additional format string
        /// </param>
        /// 
        private void FormatOutputLine(
            PSTraceSourceOptions flag,
            string classFormatter,
            string format,
            params object[] args)
        {
            try
            {
                // First format the class format string and the
                // user provided format string together

                StringBuilder output = new StringBuilder();

                if (classFormatter != null)
                {
                    output.Append(classFormatter);
                }

                if (format != null)
                {
                    output.AppendFormat(
                        CultureInfo.CurrentCulture,
                        format,
                        args);
                }

                // finally trace the output

                OutputLine(flag, output.ToString());
            }
            catch
            {
                // Eat all exceptions

                // Do not assert here because exceptions can be
                // raised while a thread is shutting down during
                // normal operation.
            }
        }
Пример #19
0
 internal ScopeTracer(PSTraceSource tracer, PSTraceSourceOptions flag, string scopeOutputFormatter, string leavingScopeFormatter, string scopeName)
 {
     this._tracer = tracer;
     this.ScopeTracerHelper(flag, scopeOutputFormatter, leavingScopeFormatter, scopeName, "", new object[0]);
 }
Пример #20
0
 private void FormatOutputLine(PSTraceSourceOptions flag, string classFormatter, string format, params object[] args)
 {
     try
     {
         StringBuilder builder = new StringBuilder();
         if (classFormatter != null)
         {
             builder.Append(classFormatter);
         }
         if (format != null)
         {
             builder.AppendFormat(Thread.CurrentThread.CurrentCulture, format, args);
         }
         this.OutputLine(flag, builder.ToString(), new object[0]);
     }
     catch
     {
     }
 }
Пример #21
0
 internal void OutputLine(PSTraceSourceOptions flag, string format, params object[] args)
 {
     if (!this.alreadyTracing)
     {
         this.alreadyTracing = true;
         try
         {
             StringBuilder lineBuilder = new StringBuilder();
             if (this.showHeaders)
             {
                 lineBuilder.Append(GetLinePrefix(flag));
             }
             AddTab(ref lineBuilder);
             if ((args != null) && (args.Length > 0))
             {
                 for (int i = 0; i < args.Length; i++)
                 {
                     if (args[i] == null)
                     {
                         args[i] = "null";
                     }
                 }
                 lineBuilder.AppendFormat(Thread.CurrentThread.CurrentCulture, format, args);
             }
             else
             {
                 lineBuilder.Append(format);
             }
             this.TraceSource.TraceInformation(lineBuilder.ToString());
         }
         finally
         {
             this.alreadyTracing = false;
         }
     }
 }
Пример #22
0
 private static StringBuilder GetLinePrefix(PSTraceSourceOptions flag)
 {
     StringBuilder builder = new StringBuilder();
     builder.AppendFormat(Thread.CurrentThread.CurrentCulture, " {0,-11} ", new object[] { Enum.GetName(typeof(PSTraceSourceOptions), flag) });
     return builder;
 }