Esempio n. 1
0
        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?.Id ?? 0;
                string   errorIdToLog = errorId ?? restExposeContext.ErrorLogId;
                string   source       = RuntimePlatformUtils.GetRequestSourceForLogging();
                string   id           = null;

                if (restExposeContext.LogTo == LogTo.RESTExpose)
                {
                    id = IntegrationLog.StaticWrite(appInfo, DateTime.Now, (int)duration.TotalMilliseconds, source, null, restExposeContext.ServiceName + "." + restExposeContext.ActionName, "REST (Expose)", errorIdToLog, true);
                }
                else
                {
                    HeContext heContext = appInfo.OsContext;
                    string    loginId   = heContext?.Session?.NewRuntimeLoginInfo?.LoginId ?? string.Empty;
                    int       userId    = heContext?.Session?.NewRuntimeLoginInfo?.UserId ?? 0;
                    string    username  = heContext?.Session?.NewRuntimeLoginInfo?.Username ?? string.Empty;

                    if (userId == 0)
                    {
                        userId   = restExposeContext.RequesterUserId;
                        username = restExposeContext.RequesterUsername;
                        loginId  = restExposeContext.RequesterLoginId;
                    }

                    if (restExposeContext.LogTo == LogTo.ScreenServices)
                    {
                        id = MobileRequestLog.StaticWrite(appInfo, DateTime.Now, restExposeContext.ScreenName, restExposeContext.ActionName, source, (int)duration.TotalMilliseconds, RuntimeEnvironment.MachineName, errorIdToLog, loginId, userId, username);
                    }
                    else if (restExposeContext.LogTo == LogTo.ServiceAPIs)
                    {
                        id = ServiceAPILog.StaticWrite(
                            appInfo: appInfo,
                            sessionInfo: heContext?.Session,
                            instant: DateTime.Now,
                            loginId: loginId,
                            errorId: errorIdToLog,
                            executedBy: RuntimeEnvironment.MachineName,
                            action: restExposeContext.ServiceAPIMethodName,
                            duration: (int)duration.TotalMilliseconds,
                            source: source,
                            endpoint: Path.Combine(appInfo.eSpaceName, "ServiceAPI", restExposeContext.ServiceAPIMethodName),
                            originalRequestKey: restExposeContext.OriginalRequestKey
                            );
                    }
                }

                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);
                    WriteResponseHeaders(responseTrace, context, actionExecutedContext);

                    if (actionExecutedContext.Response != null && actionExecutedContext.Response.Content != null)
                    {
                        if (!restExposeContext.IsResponseBinary)
                        {
                            try {
                                byte[]   content         = actionExecutedContext.Response.Content.ReadAsByteArrayAsync().Result;
                                string   charset         = RestServiceHttpUtils.TryGetResponseEncoding(actionExecutedContext.Response, DefaultEncoding);
                                Encoding requestEncoding = Encoding.GetEncoding(charset);
                                responseTrace.AppendLine(requestEncoding.GetString(content));
                            } catch {
                                WriteBinaryData(responseTrace);
                            }
                        }
                        else
                        {
                            WriteBinaryData(responseTrace);
                        }
                    }

                    int    tenantIdToDetailLog = (appInfo.Tenant != null ? appInfo.Tenant.Id : 0);
                    string detail      = $"{restExposeContext.RequestTrace}\n\n{responseTrace.ToString()}";
                    string detailLabel = "HTTP Trace";

                    StaticDetailLog(restExposeContext.LogTo, id, DateTime.Now, tenantIdToDetailLog, string.Empty, detail, detailLabel);
                }

                restExposeContext.AlreadyLogged = true;
            }
        }
Esempio n. 2
0
        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;
            }
        }