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

C# 将Trace.axd中的数据记录到text/xml文件,c#,asp.net,logging,profiling,trace,C#,Asp.net,Logging,Profiling,Trace,在尝试跟踪仅在生产环境中发生的性能问题时,我们在应用程序中启用了跟踪,因此请参见方法调用和页面加载时间 这是工作得很好,有很多信息,有助于跟踪问题。但是,查看此信息的唯一方法是浏览到Trace.axd,然后逐个查看每个请求 也只能以这种方式跟踪第一个X请求,X的最大限制为10000 是否有方法将此跟踪信息定向到文件或数据库?我相信这可以通过系统进行。诊断,但是,我运气不太好 我已经启用了使用 <trace enabled="true" writeToDiagnosticsTrace="tr

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

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

也只能以这种方式跟踪第一个X请求,X的最大限制为10000

是否有方法将此跟踪信息定向到文件或数据库?我相信这可以通过
系统进行。诊断
,但是,我运气不太好

我已经启用了使用

<trace enabled="true" writeToDiagnosticsTrace="true" />
这将生成一个xml文件,其中包含跟踪项(如“开始加载”、“结束加载”)的时间戳和数据

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

这在没有任何重大代码更改的情况下是可能的吗?理想情况下,我希望仅使用web.config更改启用此功能

另外,我还研究了其他应用程序,如RedGate和Equalec的分析工具,但是,我想首先使用非侵入性跟踪选项


该应用程序是用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());
                }
            }
        }
    }
}
更新

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

                }
            }
        }

跟踪数据由标准数据集覆盖。您无法正式掌握它,但这里有一个黑客可以做到这一点(它似乎在.NET 2到4上工作):

一旦拥有了数据集,就可以对其执行任何操作,保存到XML文件(DataSet.WriteXml)、流等


当然,由于它使用了一个内部字段,将来可能不支持它。

+1,我喜欢这个,并且很可能在我们的应用程序中实现它。然而,我所追求的其中一件事是尽可能多地提供关于请求的信息,包括post和会话数据。我们可以发现某些页面速度很慢,但我们遇到了这样的情况:某些用户执行了导致问题的某些操作。我们目前不知道哪些信息有用,因此希望查看所有信息以查找模式。我已经附加了我使用的转储代码,我通常会根据警告/错误构建一个html文件,这样看起来很容易。
        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) {

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