Logging the data in Trace.axd to a text/xml file

后端 未结 2 1055
闹比i
闹比i 2021-01-20 15:20

In trying to track down a performance issue that is only occurring in our production environment, we have enabled tracing within the app so see method calls and page load ti

相关标签:
2条回答
  • 2021-01-20 15:57

    I have a standard HttpModule i use for all my web applications to monitor performance. The logger used can be changed, also can do things like remove white space from source, compress, or send emails if certain limits are reached. Its easier than trawling through asp.net traces as you get only the information you decide is importent.

    public class PerfHttpModule : IHttpModule {
    
        private static Common.Logging.ILog log = Common.Logging.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public static readonly string CONTEXT_RequestStart = "PerfHttpModule_RequestStart";
        public static readonly string CONTEXT_RequestId = "PerfHttpModule_RequestId";
    
        public void Init(HttpApplication context) {
            context.BeginRequest += new EventHandler(context_BeginRequest);
            context.EndRequest += new EventHandler(context_EndRequest);
        }
    
        private void context_BeginRequest(object sender, EventArgs e) {
            try {
                if (HttpContext.Current != null) {
                    HttpContext.Current.Items[CONTEXT_RequestStart] = DateTime.Now;
                    HttpContext.Current.Items[CONTEXT_RequestId] = random.Next(999999).ToString("D6");
                    log.Info("Url: " + HttpContext.Current.Request.Url + " (" + HttpContext.Current.Request.ContentLength + ")");
                }
            } catch {
            }
        }
    
        private void context_EndRequest(object sender, EventArgs e) {
            if (HttpContext.Current.Items.Contains(CONTEXT_RequestStart)) {
                DateTime time1 = (DateTime)HttpContext.Current.Items[CONTEXT_RequestStart];
                DateTime time2 = DateTime.Now;
                double ms = (time2 - time1).TotalMilliseconds;
                log.Info("TotalMilliseconds: " + ms);
                if (ms > AppSettings.SlowPage || ms > AppSettings.ErrorSlowPage) {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("Slow page detected." + "\t");
                    sb.Append("TotalMilliseconds: " + ms + "\t");
                    sb.Append("Url: " + HttpContext.Current.Request.Url.ToString());
                    if (ms > AppSettings.ErrorSlowPage) {
                        log.Error(sb.ToString());
                    } else if (ms > AppSettings.SlowPage) {
                        log.Warn(sb.ToString());
                    }
                }
            }
        }
    }
    

    UPDATE

            if (HttpContext.Current != null) {
                    NameValueCollection tmp = new NameValueCollection(HttpContext.Current.Request.ServerVariables);
                    foreach (string i in tmp.Keys) {
    
                    }
                if (HttpContext.Current.Server != null) {
                    if (HttpContext.Current.Server.GetLastError() != null) {
    
                    }
                }
                if (HttpContext.Current.Session != null) {
                    foreach (string i in HttpContext.Current.Session.Keys) {
    
                    }
                }
                if (HttpContext.Current.Request.Cookies != null) {
                    foreach (string i in HttpContext.Current.Request.Cookies.Keys) {
    
                    }
                }
                if (HttpContext.Current.Response.Cookies != null) {
                    foreach (string i in HttpContext.Current.Response.Cookies.Keys) {
    
                    }
                }
                if (HttpContext.Current.Items != null) {
                    foreach (string i in HttpContext.Current.Items.Keys) {
    
                    }
                }
                if (HttpContext.Current.Request.Form != null) {
                    foreach (string i in HttpContext.Current.Request.Form.Keys) {
    
                    }
                }
            }
    
    0 讨论(0)
  • 2021-01-20 15:57

    The trace data is under cover a standard DataSet. You can't get a hold on it officially, but here is a hack that can do it (it seems to work on .NET 2 to 4):

    public static DataSet GetTraceData(Page page)
    {
        if (page == null)
            throw new ArgumentNullException("page");
    
        return (DataSet)typeof(TraceContext).GetField("_requestData",
               BindingFlags.NonPublic | BindingFlags.Instance).GetValue(page.Trace);
    }
    

    Once you have the DataSet, you can do anything you want with it, save to an XML file (DataSet.WriteXml), a stream, etc.

    Of course, since it uses an internal field, it may not be supported in the future.

    0 讨论(0)
提交回复
热议问题