将Trace.axd中的数据记录到text / xml文件中

在尝试追踪仅在我们的生产环境中发生的性能问题时,我们在应用程序中启用了跟踪,因此请查看方法调用和页面加载时间。

这很有效,有很多信息可以帮助追踪问题。 但是,查看此信息的唯一方法是浏览到Trace.axd,然后单独查看每个请求。

它也只能以这种方式跟踪前X个请求,X的最大限制为10,000。

有没有办法将此跟踪信息定向到文件或数据库? 我相信这可以使用System.Diagnostics来完成,但是,我没有太多运气。

我已启用跟踪使用

  

我尝试过使用XmlWriterTraceListener

         

这会生成一个xml文件,其中包含跟踪项的时间戳和数据,例如“Begin Load”,“End Load”等。

但是,它似乎只记录单个请求,并且不记录所有请求。 此外,虽然加载时间很有用,但理想情况下我希望能够获得Trace.axd中可以看到的所有信息,例如请求数据,发布数据,会话数据等。

这是否可能没有任何重大代码更改? 理想情况下,我想仅使用web.config更改来启用此function。

或者,我已经研究过其他应用程序,例如RedGate和Equatec的分析工具,但是,我想首先用尽非侵入式跟踪选项。

该应用程序是用ASP.Net 3.5和C#编写的。

我有一个标准的HttpModule用于我的所有Web应用程序来监控性能。 使用的记录器可以更改,也可以执行诸如从源中删除空格,压缩或发送电子邮件(如果达到某些限制)的操作。 它比浏览asp.net跟踪更容易,因为您只获得了您认为重要的信息。

 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) { } } } 

跟踪数据涵盖标准DataSet。 你无法正式获得它,但这是一个可以做到它的黑客(它似乎适用于.NET 2到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); } 

拥有DataSet后,您可以使用它执行任何操作,保存到XML文件(DataSet.WriteXml),流等。

当然,由于它使用内部字段,因此将来可能不会支持它。