c# - 将 Trace.axd 中的数据记录到文本/xml 文件

标签 c# asp.net logging profiling trace

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

这运作良好,并且有很多信息可以帮助追踪问题。但是,查看此信息的唯一方法是浏览到 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 文件,其中包含“开始加载”、“结束加载”等跟踪项的时间戳和数据。

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

如果不对代码进行任何重大更改,这是否可能?理想情况下,我希望仅使用 web.config 更改来启用它。

或者,我研究了其他应用程序,例如 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());
                }
            }
        }
    }
}

更新

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

                }
            }
        }

关于c# - 将 Trace.axd 中的数据记录到文本/xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4735173/

相关文章:

c# - 使用 CodeBehind 中的数据填充 GridView 中模板字段内的 DropDownList

.net - 我需要一种快速而肮脏的方法来附加到 vb.net 中的文本文件

asp.net - Log4net 未记录到文件

c# - 具有可选参数/默认参数值的 ASP.NET5 MVC 6 路由

c# - 如何在 ASP.NET/C# 中将数据表转换为字典

c# - 以编程方式在 Asp.Net ListView 中选择项目

c# - 当 gridview 再次获取数据绑定(bind)时,如何保留在 gridview 模板字段内的 TextBox 中输入的值?

python - 如何更改 python 应用程序引擎中日志消息的默认格式?

c# - 如何加速这种加密方法 C# 文件流

c# - 从数据库表中删除行