2011-01-19 35 views
3

在试图追踪仅在我们的生产环境中发生的性能问题时,我们在应用程序内启用了跟踪,以查看方法调用和页面加载时间。将Trace.axd中的数据记录到文本/ xml文件中

这工作得很好,有很多信息有助于追踪问题。但是,查看此信息的唯一方法是浏览到Trace.axd,然后分别查看每个请求。

也可以这样跟踪第一个X请求,X的最大限制为10,000。

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

我已经启用了使用

<trace enabled="true" writeToDiagnosticsTrace="true" /> 

我已经使用了XmlWriterTraceListener使用

<system.diagnostics> 
    <trace autoflush="true"> 
     <listeners> 
     <add 
      name="XmlWriterTraceListener" 
      type="System.Diagnostics.XmlWriterTraceListener" 
      initializeData="c:\trace.xml" 
      /> 
     </listeners> 
    </trace> 
    </system.diagnostics> 

这导致包含时间戳和数据跟踪项目,如XML文件试图追踪“开始加载“,”End Load“等。

但是,它似乎只记录一个请求,并没有记录所有请求。此外,尽管加载时间有用,但理想情况下,我希望提供Trace.axd中可以看到的所有信息,例如请求数据,发布数据,会话数据等。

这是可能根本没有任何主要的代码更改?理想情况下,我想启用此只使用web.config更改。

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

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

回答

4

我有一个标准的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) { 

       } 
      } 
     } 
+0

+1,我喜欢这个,可以在我们的应用程序中实现它。然而,我所追求的内容之一是尽可能多地了解有关请求的信息,包括帖子和会话数据。我们可以发现某些页面很慢,但是我们遇到了一些用户执行了一些导致问题的操作的情况。我们目前不知道哪些信息会有用,因此希望查看所有信息以查找模式。 – 2011-01-19 12:56:12

2

的跟踪数据是下盖一个标准数据集。你不能得到它保持正式,但在这里是可以做到这一点(它似乎在.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); 
} 

一旦你的数据集,你可以做任何事情你想用它,保存到一个XML文件(DataSet.WriteXml),一个流等。

当然,因为它使用了一个内部字段,它可能在将来不被支持。

相关问题