/// <summary> /// Event handler for when we need to collect web request details /// </summary> /// <param name="detail"></param> internal static void WebRequestDetail_SetWebRequestDetail(WebRequestDetail detail) { //make sure we have access to the context var context = Configure.ServiceProvider?.GetService <IHttpContextAccessor>()?.HttpContext; if (context == null) { return; } Load(context, detail); }
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) { var httpRequest = HttpContextAccessor.HttpContext; if (httpRequest != null) { #if NETFULL WebRequestDetail webRequest = new WebRequestDetail(); webRequest.Load(httpRequest); string result = JsonConvert.SerializeObject(webRequest); builder.Append(result); #endif } }
private static void Load(HttpContext context, WebRequestDetail detail) { if (context == null || context.Request == null) { return; } var request = context.Request; try { detail.HttpMethod = request.Method; detail.UserIPAddress = context.Connection?.RemoteIpAddress?.ToString(); detail.RequestProtocol = request.IsHttps ? "https" : "http"; detail.RequestUrl = $"{detail.RequestProtocol}//{request.Host}{request.Path}"; detail.MVCAction = context.GetRouteValue("action")?.ToString(); detail.MVCController = context.GetRouteValue("controller")?.ToString(); if (string.IsNullOrEmpty(detail.MVCAction) == false && string.IsNullOrEmpty(detail.MVCController) == false) { detail.ReportingUrl = $"{detail.MVCController}.{detail.MVCAction}"; } } catch (Exception) { // ignored } try { if (request.QueryString != null) { detail.QueryString = ToKeyValues(request.Query, null, null); } if (request.Headers != null && Config.CaptureErrorHeaders) { if (Config.ErrorHeaderBadKeys == null) { Config.ErrorHeaderBadKeys = new List <string>(); } if (Config.ErrorHeaderBadKeys.Contains("cookie") == false) { Config.ErrorHeaderBadKeys.Add("cookie"); } if (Config.ErrorHeaderBadKeys.Contains("authorization") == false) { Config.ErrorHeaderBadKeys.Add("authorization"); } detail.Headers = ToKeyValues(request.Headers, Config.ErrorHeaderGoodKeys, Config.ErrorHeaderBadKeys); } if (request.Cookies != null && Config.CaptureErrorCookies) { detail.Cookies = ToKeyValues(request.Cookies, Config.ErrorCookiesGoodKeys, Config.ErrorCookiesBadKeys); } if (request.Form != null && Config.CaptureErrorPostdata) { detail.PostData = ToKeyValues(request.Form, null, null); } } catch (Exception) { // ignored } }
private void Init() { TimeSpan ts = DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, 0)); OccurredEpochMillis = (long)ts.TotalMilliseconds; EnvironmentDetail = EnvironmentDetail.Get(false); ServerVariables = new Dictionary <string, string>(); #if NETFULL if (System.Web.HttpContext.Current != null) { // Make sure that the request is available in the current context. bool requestAvailable = false; try { if (System.Web.Hosting.HostingEnvironment.IsHosted && System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.Handler != null && System.Web.HttpContext.Current.Request != null) { HttpRequest request = System.Web.HttpContext.Current.Request; if (request != null) { requestAvailable = true; } } } catch { // Web request may not be available at this time. // Example: In the Application_Start method, there is an HttpContext, but no HttpContext.Request // System.Web.HttpException - Request is not available in this context // Do nothing } // Attach the web request details if (requestAvailable) { WebRequestDetail = new WebRequestDetail(this); if (HttpContext.Current.User != null && HttpContext.Current.User.Identity != null) { UserName = HttpContext.Current.User.Identity.Name; } } } #else WebRequestDetail = new WebRequestDetail(this); #endif //Fire event OnCaptureDetail?.Invoke(this); if (WebRequestDetail != null && WebRequestDetail.HttpMethod == null) { WebRequestDetail = null; } }
private static void Load(HttpContext context, WebRequestDetail detail) { if (context == null || context.Request == null) { return; } HttpRequest request = context.Request; try { detail.HttpMethod = request.Method; detail.UserIPAddress = context?.Connection?.RemoteIpAddress?.ToString(); //if (context.Items != null && context.Items.Contains("Stackify.ReportingUrl")) //{ // ReportingUrl = context.Items["Stackify.ReportingUrl"].ToString(); //} if (request.IsHttps) { detail.RequestProtocol = "https"; } else { detail.RequestProtocol = "http"; } detail.RequestUrl = detail.RequestProtocol + "//" + request.Host + request.Path; detail.MVCAction = context.GetRouteValue("action")?.ToString(); detail.MVCController = context.GetRouteValue("controller")?.ToString(); if (!string.IsNullOrEmpty(detail.MVCAction) && !string.IsNullOrEmpty(detail.MVCController)) { detail.ReportingUrl = detail.MVCController + "." + detail.MVCAction; } //if (request.AppRelativeCurrentExecutionFilePath != null) //{ // RequestUrlRoot = request.AppRelativeCurrentExecutionFilePath.TrimStart('~'); //} } catch (Exception) { } try { if (request.QueryString != null) { detail.QueryString = ToKeyValues(request.Query, null, null); } if (request.Headers != null && Config.CaptureErrorHeaders) { if (Config.ErrorHeaderBadKeys == null) { Config.ErrorHeaderBadKeys = new List <string>(); } if (!Config.ErrorHeaderBadKeys.Contains("cookie")) { Config.ErrorHeaderBadKeys.Add("cookie"); } if (!Config.ErrorHeaderBadKeys.Contains("authorization")) { Config.ErrorHeaderBadKeys.Add("authorization"); } detail.Headers = ToKeyValues(request.Headers, Config.ErrorHeaderGoodKeys, Config.ErrorHeaderBadKeys); } if (request.Cookies != null && Config.CaptureErrorCookies) { detail.Cookies = ToKeyValues(request.Cookies, Config.ErrorCookiesGoodKeys, Config.ErrorCookiesBadKeys); } if (request.Form != null && Config.CaptureErrorPostdata) { detail.PostData = ToKeyValues(request.Form, null, null); } //sessions return a byte array... //if (context.Session != null && Config.CaptureSessionVariables && Config.ErrorSessionGoodKeys.Any()) //{ // SessionData = new Dictionary<string, string>(); // foreach (var key in Config.ErrorSessionGoodKeys) // { // SessionData[key] = context.Session // } //} //if (Config.CaptureErrorPostdata) //{ // var contentType = context.Request.Headers["Content-Type"]; // if (contentType != "text/html" && contentType != "application/x-www-form-urlencoded" && // context.Request.RequestType != "GET") // { // int length = 4096; // string postBody = new StreamReader(context.Request.InputStream).ReadToEnd(); // if (postBody.Length < length) // { // length = postBody.Length; // } // PostDataRaw = postBody.Substring(0, length); // } //} } catch (Exception) { } }
private void Init() { TimeSpan ts = DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, 0)); OccurredEpochMillis = (long)ts.TotalMilliseconds; EnvironmentDetail = EnvironmentDetail.Get(false); ServerVariables = new Dictionary<string, string>(); if (System.Web.HttpContext.Current != null) { // Make sure that the request is available in the current context. bool requestAvailable = false; try { HttpRequest request = System.Web.HttpContext.Current.Request; if (request != null) { requestAvailable = true; } } catch { // Web request may not be available at this time. // Example: In the Application_Start method, there is an HttpContext, but no HttpContext.Request // System.Web.HttpException - Request is not available in this context // Do nothing } // Attach the web request details if (requestAvailable) { WebRequestDetail = new WebRequestDetail(this); if (HttpContext.Current.User != null && HttpContext.Current.User.Identity != null) { UserName = HttpContext.Current.User.Identity.Name; } } } //Fire event if (OnCaptureDetail != null) { OnCaptureDetail(this); } }