/// <summary> /// Starts a new MiniProfiler and associates it with the current <see cref="HttpContext.Current"/>. /// </summary> public override MiniProfiler Start() { var context = HttpContext.Current; if (context == null) { return(null); } var url = context.Request.Url; var path = context.Request.AppRelativeCurrentExecutionFilePath.Substring(1).ToUpperInvariant(); var name = context.Request.Url.Segments[context.Request.Url.Segments.Length - 1].ToString(); var result = new MiniProfiler(name); Current = result; SetProfilerActive(result); result.Action = HttpContext.Current.Request.HttpMethod.ToUpper(); result.Url = url.OriginalString; result.Event = GetEvent(); result.Referer = context.Request.ServerVariables["HTTP_REFERER"]; result.ClientIP = context.Request.ServerVariables["REMOTE_ADDR"]; result.ClientAgent = context.Request.ServerVariables["HTTP_USER_AGENT"]; result.RequestID = Guid.NewGuid().ToString().ToUpper(); result.RequestSize = HttpContext.Current.Request.TotalBytes; result.ActiveUser = ""; result.Exception = ""; // Read cookie with client timings if (HttpContext.Current != null && HttpContext.Current.Request != null) { HttpCookie trackingCookie = HttpContext.Current.Request.Cookies["CorpNetProfilerTracking"]; if (trackingCookie != null) { string[] parts = trackingCookie.Value.Split('|'); // Malformed? if (parts.Length == 9) { string requestID = parts[0].ToString(); Guid guid; // Only save if the requestID is a valid GUID if (Guid.TryParse(requestID, out guid)) { result.ClientRequestID = requestID; result.ClientTotalDuration = parts[1].ToDecimal(); result.ClientRedirectDuration = parts[2].ToDecimal(); result.ClientDnsDuration = parts[3].ToDecimal(); result.ClientConnectionDuration = parts[4].ToDecimal(); result.ClientRequestDuration = parts[5].ToDecimal(); result.ClientResponseDuration = parts[6].ToDecimal(); result.ClientDomDuration = parts[7].ToDecimal(); result.ClientLoadDuration = parts[8].ToDecimal(); } } } } // Add session ID if (HttpContext.Current != null && HttpContext.Current.Session != null) { result.SessionID = HttpContext.Current.Session.SessionID; } // Add header id response header (for tracing on IIS) context.Response.AddHeader("Request-ID", result.RequestID); // Obtain activeuser from session if (!String.IsNullOrEmpty(Settings.ActiveUserLocator)) { result.ActiveUser = GetActiveUser(Settings.ActiveUserLocator); } return(result); }
public static IDisposable Step(this MiniProfiler profiler, string name) { return(profiler == null ? null : profiler.StepImpl(name, "")); }