private static ContextInfoForStack FillContextInfoStruct(HeContext context) { ContextInfoForStack contextForStack = new ContextInfoForStack(); if (context != null) { try { contextForStack.sessionId = context.Session.SessionID; } catch { } try { contextForStack.userId = context.Session.UserId; } catch { } try { contextForStack.username = context.Session.UserName; } catch { } try { if (context.RequestTracer != null) { RequestTracer requestTracer = context.RequestTracer; contextForStack.requestKey = requestTracer.RequestKey; contextForStack.actionName = requestTracer.EntryActionName; contextForStack.entryPointName = requestTracer.EntryEndpointName; requestTracer.RegisterError(); } } catch { } try { contextForStack.requestUrl = context.Context.Request.Url.ToString(); } catch { } try { contextForStack.requestMethod = context.Context.Request.HttpMethod; } catch { } try { contextForStack.requestQuery = context.Context.Request.QueryString != null?context.Context.Request.QueryString.ToString() : ""; } catch { } try { contextForStack.currentLocale = context.CurrentLocale; } catch { } try { contextForStack.clientIp = RuntimePlatformUtils.GetClientIpForLogging(); } catch { } try { contextForStack.deviceUuid = RuntimePlatformUtils.GetDeviceUuidForLogging(); } catch { } try { contextForStack.CurrentExecutionFileName = context.CurrentExecutionFileName; } catch { } } return(contextForStack); }
public static void LogResponse(HttpActionExecutedContext actionExecutedContext, string errorId = null) { if (actionExecutedContext == null || actionExecutedContext.Response == null) { return; } AppInfo appInfo = AppInfo.GetAppInfo(); HttpContext context = appInfo.Context; RESTExposeContext restExposeContext = GetLoggingContext(); if (restExposeContext != null && !restExposeContext.AlreadyLogged && ((!string.IsNullOrEmpty(restExposeContext.ErrorLogId) || (!appInfo.SelectiveLoggingEnabled || appInfo.Properties.AllowLogging)) && restExposeContext.LogRequest)) { TimeSpan duration = DateTime.Now.Subtract(restExposeContext.StartTime); int eSpaceId = appInfo.eSpaceId; int tenantId = (appInfo.Tenant != null ? appInfo.Tenant.Id : 0); string errorIdToLog = errorId ?? restExposeContext.ErrorLogId; string source = RuntimePlatformUtils.GetClientIpForLogging(); string id = null; if (restExposeContext.IsScreenService) { HeContext heContext = appInfo.OsContext; string loginId = heContext.Session.NewRuntimeLoginInfo.LoginId; int userId = heContext.Session.NewRuntimeLoginInfo.UserId; id = MobileRequestLog.StaticWrite(DateTime.Now, eSpaceId, tenantId, restExposeContext.ScreenName, restExposeContext.ActionName, source, (int)duration.TotalMilliseconds, Environment.MachineName, errorIdToLog, loginId, userId); } else { id = IntegrationLog.StaticWrite(DateTime.Now, (int)duration.TotalMilliseconds, source, null, restExposeContext.ServiceName + "." + restExposeContext.ActionName, "REST (Expose)", eSpaceId, tenantId, errorIdToLog, Environment.MachineName, true); } bool withError = !string.IsNullOrEmpty(errorIdToLog); if (actionExecutedContext != null && restExposeContext.RequestTrace != null && (restExposeContext.TraceAll || (restExposeContext.TraceErrors && withError))) { StringBuilder responseTrace = new StringBuilder(); responseTrace.AppendLine(context.Request.ServerVariables["SERVER_PROTOCOL"] + " " + ((int)actionExecutedContext.Response.StatusCode).ToString() + " " + actionExecutedContext.Response.ReasonPhrase); if (HttpRuntime.UsingIntegratedPipeline) { foreach (var headerName in context.Response.Headers.AllKeys) { foreach (string headerValue in context.Response.Headers.GetValues(headerName)) { responseTrace.AppendLine(headerName + ": " + headerValue); } } } if (actionExecutedContext.Response != null && actionExecutedContext.Response.Content != null) { foreach (var header in actionExecutedContext.Response.Content.Headers) { foreach (string headerValue in header.Value) { responseTrace.AppendLine(header.Key + ": " + headerValue); } } if (restExposeContext.IsResponseBinary) { responseTrace.AppendLine("<BINARY DATA>"); } else { try { byte[] content = actionExecutedContext.Response.Content.ReadAsByteArrayAsync().Result; string charset = RestServiceHttpUtils.TryGetResponseEncoding(actionExecutedContext.Response, "utf-8"); Encoding requestEncoding = Encoding.GetEncoding(charset); responseTrace.AppendLine(requestEncoding.GetString(content)); } catch { responseTrace.AppendLine("<BINARY DATA>"); } } } int tenantIdToDetailLog = (appInfo.Tenant != null ? appInfo.Tenant.Id : 0); string detail = restExposeContext.RequestTrace + "\n\n" + responseTrace.ToString(); string detailLabel = "HTTP Trace"; if (restExposeContext.IsScreenService) { MRDetailLog.StaticWrite(id, DateTime.Now, tenantIdToDetailLog, "", detail, detailLabel); } else { IntDetailLog.StaticWrite(id, DateTime.Now, tenantIdToDetailLog, "", detail, detailLabel); } } restExposeContext.AlreadyLogged = true; } }