Writing data in Trace.axd to a text / xml file

When trying to track a performance issue that only occurs in our production environment, we turned on in-app tracking, so see method calls and page load times.

This works well, and there is a lot of information that helps track problems. However, the only way to view this information is to view Trace.axd and then view each request individually.

It is also possible to track the first X requests this way, and X has a maximum limit of 10,000.

Is there a way to direct this trace information to a file or database? I believe that this can be done with help System.Diagnostics, but I was not very lucky.

I turned on tracing with

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

I tried using XmlWriterTraceListenerusing

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

The result is an xml file containing timestamps and data for trace elements such as "Start Download", "Final Download", etc.

However, it seems that the log only logs one request and does not log all requests. Also, while load times are useful, ideally I would like to have all the information that can be seen in Trace.axd, such as request data, mail data, session data, etc.

Is this possible at all without any major code changes? Ideally, I would like to enable this using only web.config changes.

As an alternative, I looked at other applications, such as the RedGate and Equatec profiling tools, however I would like to fix the non-invasive trace parameters first.

ASP.Net 3.5 #.

+3
2

HttpModule, - . , , , , . , 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) {

                }
            }
        }
+4

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), ..

, , .

+2

Source: https://habr.com/ru/post/1786387/


All Articles