private static void StaticDetailLog(LogTo logTo, string id, DateTime instant, int tenant_Id, string message, string detail, string detailLabel) { switch (logTo) { case LogTo.RESTExpose: IntDetailLog.StaticWrite(id, instant, tenant_Id, message, detail, detailLabel); break; case LogTo.ScreenServices: MRDetailLog.StaticWrite(id, instant, tenant_Id, message, detail, detailLabel); break; case LogTo.ServiceAPIs: ServiceAPIDetailLog.StaticWrite(id, instant, tenant_Id, message, detail, detailLabel); break; default: break; } }
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; } }