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

后端 未结 2 1082
闹比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) {
    
                    }
                }
            }
    

提交回复
热议问题