示例#1
0
        private IDisposable WriteStartElement(string title, IDictionary <string, string> attribs)
        {
            try
            {
                var info = new TraceInfo(title, attribs);

                if (String.IsNullOrEmpty(_file))
                {
                    EnsureMaxXmlFiles();

                    // generate trace file name base on attribs
                    _file = GenerateFileName(info);
                }

                var strb = new StringBuilder();

                if (_isStartElement)
                {
                    strb.AppendLine(">");
                }

                strb.Append(new String(' ', _infos.Count * 2));
                strb.AppendFormat("<step title=\"{0}\" ", XmlUtility.EscapeXmlText(title));
                strb.AppendFormat("date=\"{0}\" ", DateTime.UtcNow.ToString("yyy-MM-ddTHH:mm:ss.fff"));
                if (_infos.Count == 0)
                {
                    strb.AppendFormat("instance=\"{0}\" ", InstanceIdUtility.GetShortInstanceId());
                }

                foreach (var attrib in attribs)
                {
                    if (TraceExtensions.IsNonDisplayableAttribute(attrib.Key))
                    {
                        continue;
                    }

                    strb.AppendFormat("{0}=\"{1}\" ", attrib.Key, XmlUtility.EscapeXmlText(attrib.Value));
                }

                FileSystemHelpers.AppendAllTextToFile(_file, strb.ToString());
                _infos.Push(info);
                _isStartElement = true;

                return(new DisposableAction(() => WriteEndTrace()));
            }
            catch (Exception ex)
            {
                WriteUnexpectedException(ex);
            }

            return(DisposableAction.Noop);
        }
示例#2
0
        public void Trace(string message, IDictionary <string, string> attributes)
        {
            // these are traced by TraceModule
            if (message == XmlTracer.IncomingRequestTrace ||
                message == XmlTracer.OutgoingResponseTrace)
            {
                return;
            }

            if (_requestMethod == HttpMethod.Get.Method)
            {
                // ignore normal GET request body
                string type = null;
                if (!attributes.TryGetValue("type", out type))
                {
                    return;
                }

                if (type != "error" && type != "warning")
                {
                    return;
                }
            }


            var strb = new StringBuilder();

            strb.AppendFormat("{0} ", message);

            // took from XMLtracer
            foreach (var attrib in attributes)
            {
                if (TraceExtensions.IsNonDisplayableAttribute(attrib.Key))
                {
                    continue;
                }

                strb.AppendFormat("{0}=\"{1}\" ", attrib.Key, attrib.Value);
            }

            KuduEventSource.Log.GenericEvent(ServerConfiguration.GetRuntimeSiteName(),
                                             strb.ToString(),
                                             _requestId,
                                             string.Empty,
                                             string.Empty,
                                             string.Empty);
        }
示例#3
0
            public void WriteLine(string value, IDictionary <string, string> attributes, int depth, bool end = false)
            {
                string type;

                attributes.TryGetValue("type", out type);

                if (type == "request" && end)
                {
                    // add for delay cleanup
                    CleanupHelper.AddFile(_logFile);
                }

                if (!_tracer.ShouldTrace(attributes))
                {
                    return;
                }

                if (FilterTrace(attributes, out attributes))
                {
                    return;
                }

                var strb = new System.Text.StringBuilder();

                strb.Append(DateTime.UtcNow.ToString("s"));
                strb.Append(GetIndentation(depth + 1));

                if (end)
                {
                    strb.Append("Done ");
                }

                if (type == "process")
                {
                    strb.Append(attributes["path"]);
                    if (!end)
                    {
                        strb.Append(GetIndentation(1));
                        strb.Append(attributes["arguments"]);
                    }
                }
                else
                {
                    strb.Append(value);

                    if (!end)
                    {
                        foreach (KeyValuePair <string, string> pair in attributes)
                        {
                            if (TraceExtensions.IsNonDisplayableAttribute(pair.Key))
                            {
                                continue;
                            }

                            strb.AppendFormat(", {0}: {1}", pair.Key, pair.Value);
                        }
                    }

                    if (type == "request")
                    {
                        if (!end)
                        {
                            _stopWatch = Stopwatch.StartNew();
                        }
                        else
                        {
                            _stopWatch.Stop();
                            strb.AppendFormat(", elapsed: {0} ms", _stopWatch.ElapsedMilliseconds);
                            strb.AppendLine();
                        }
                    }
                }

                using (StreamWriter writer = new StreamWriter(_fileSystem.File.Open(_logFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite)))
                {
                    writer.WriteLine(strb.ToString());
                }
            }
示例#4
0
        public IDisposable Step(string title, IDictionary <string, string> attributes)
        {
            var newStep        = new TraceStep(title);
            var newStepElement = new XElement("step", new XAttribute("title", title),
                                              new XAttribute("date", DateTime.UtcNow.ToString("MM/dd H:mm:ss")));

            foreach (var pair in attributes)
            {
                if (TraceExtensions.IsNonDisplayableAttribute(pair.Key))
                {
                    continue;
                }

                string safeValue = XmlUtility.Sanitize(pair.Value);
                newStepElement.Add(new XAttribute(pair.Key, safeValue));
            }

            if (_currentSteps.Count == 0)
            {
                // Add a new top level step
                _steps.Add(newStep);
            }

            _currentSteps.Push(newStep);
            _elements.Push(newStepElement);

            // Start profiling
            newStep.Start();

            return(new DisposableAction(() =>
            {
                try
                {
                    // If there's no steps then do nothing (guard against double dispose)
                    if (_currentSteps.Count == 0)
                    {
                        return;
                    }

                    // Stop the current step
                    _currentSteps.Peek().Stop();

                    TraceStep current = _currentSteps.Pop();
                    XElement stepElement = _elements.Pop();

                    stepElement.Add(new XAttribute("elapsed", current.ElapsedMilliseconds));

                    if (_elements.Count > 0)
                    {
                        XElement parent = _elements.Peek();
                        parent.Add(stepElement);
                    }
                    else if (ShouldTrace(stepElement.LastNode as XElement))
                    {
                        // Add this element to the list
                        Save(stepElement);
                    }

                    if (_currentSteps.Count > 0)
                    {
                        TraceStep parent = _currentSteps.Peek();
                        parent.Children.Add(current);
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }));
        }
示例#5
0
        private IDisposable WriteStartElement(string title, IDictionary <string, string> attribs)
        {
            try
            {
                var info = new TraceInfo(title, attribs);

                var ensureMaxXmlFiles = false;
                if (String.IsNullOrEmpty(_file))
                {
                    ensureMaxXmlFiles = true;

                    // generate trace file name base on attribs
                    _file = GenerateFileName(info);
                    if (!startTagAdded)
                    {
                        log.Debug("@@@StartTrace@@@" + DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff",
                                                                                CultureInfo.InvariantCulture) + "," + ServerConfiguration.GetApplicationName() +
                                  ",");
                        startTagAdded = true;
                    }
                }

                var strb = new StringBuilder();

                if (_isStartElement)
                {
                    strb.AppendLine(">");
                }

                strb.Append(new String(' ', _infos.Count * 2));
                strb.AppendFormat("<step title=\"{0}\" ", XmlUtility.EscapeXmlText(title));
                strb.AppendFormat("date=\"{0}\" ", info.StartTime.ToString("yyyy-MM-ddTHH:mm:ss.fff"));
                if (_infos.Count == 0)
                {
                    strb.AppendFormat("instance=\"{0}\" ", InstanceIdUtility.GetShortInstanceId());
                }

                foreach (var attrib in attribs)
                {
                    if (TraceExtensions.IsNonDisplayableAttribute(attrib.Key))
                    {
                        continue;
                    }

                    strb.AppendFormat("{0}=\"{1}\" ", attrib.Key, XmlUtility.EscapeXmlText(attrib.Value));
                }

                FileSystemHelpers.AppendAllTextToFile(_file, strb.ToString());
                log.Debug(Regex.Replace(strb.ToString(), @"\t|\n|\r", ""));
                _infos.Push(info);
                _isStartElement = true;

                if (ensureMaxXmlFiles)
                {
                    EnsureMaxXmlFiles();
                }

                return(new DisposableAction(() => WriteEndTrace()));
            }
            catch (Exception ex)
            {
                WriteUnexpectedException(ex);
            }

            return(DisposableAction.Noop);
        }