示例#1
0
        public static void LogRequest(HttpActionContext actionContext)
        {
            RESTExposeContext restExposeContext = GetLoggingContext();

            if (restExposeContext != null && (restExposeContext.TraceAll || restExposeContext.TraceErrors))
            {
                HttpContext   context      = HttpContext.Current;
                StringBuilder requestTrace = new StringBuilder();
                requestTrace.AppendLine(context.Request.HttpMethod + " " + context.Request.Url.ToString() + " " + context.Request.ServerVariables["SERVER_PROTOCOL"]);
                WriteRequestHeaders(requestTrace, context);

                if (actionContext.Request.Content != null)
                {
                    byte[] content = actionContext.Request.Content.ReadAsByteArrayAsync().Result;
                    if (!restExposeContext.IsRequestBinary)
                    {
                        try {
                            string   charset         = RestServiceHttpUtils.TryGetRequestEncoding(actionContext.Request, DefaultEncoding);
                            Encoding requestEncoding = Encoding.GetEncoding(charset);
                            requestTrace.AppendLine(requestEncoding.GetString(content));
                        } catch {
                            WriteBinaryData(requestTrace);
                        }
                    }
                    else
                    {
                        WriteBinaryData(requestTrace);
                    }
                }

                restExposeContext.RequestTrace = requestTrace.ToString();
            }
        }
示例#2
0
        public static void SetupLoggingContext(HttpActionContext actionContext, string serviceName, string ssKey)
        {
            RESTExposeContext restExposeContext = new RESTExposeContext {
                StartTime   = DateTime.Now,
                ServiceName = serviceName
            };

            // Try to set any of the RESTContexts
            // The REST Methods should only have one configuration with the following order:
            // 1. Expose REST API Methods
            // 2. Screen Services Actions
            // 3. ServiceAPIMethods
            // If none was set (returned true) then we are not adding this context to the HTTPContext (no logging will be processed)
            if (TrySetRESTContextForRESTExposeActions(actionContext, ssKey, ref restExposeContext) ||
                TrySetRESTContextForScreenServicesActions(actionContext, ref restExposeContext) ||
                TrySetRESTContextForServiceAPIMethods(actionContext, serviceName, ssKey, ref restExposeContext))
            {
                HttpContext.Current.Items[RESTContextVariableName] = restExposeContext;
            }
        }
示例#3
0
        public static void LogRequest(HttpActionContext actionContext)
        {
            RESTExposeContext restExposeContext = GetLoggingContext();

            if (restExposeContext != null && (restExposeContext.TraceAll || restExposeContext.TraceErrors))
            {
                HttpContext   context      = HttpContext.Current;
                StringBuilder requestTrace = new StringBuilder();
                requestTrace.AppendLine(context.Request.HttpMethod + " " + context.Request.Url.ToString() + " " + context.Request.ServerVariables["SERVER_PROTOCOL"]);
                foreach (string headerName in context.Request.Headers.Keys)
                {
                    foreach (string headerValue in context.Request.Headers.GetValues(headerName))
                    {
                        requestTrace.AppendLine(headerName + ": " + headerValue);
                    }
                }

                if (actionContext.Request.Content != null)
                {
                    byte[] content = actionContext.Request.Content.ReadAsByteArrayAsync().Result;
                    if (!restExposeContext.IsRequestBinary)
                    {
                        try {
                            string   charset         = RestServiceHttpUtils.TryGetRequestEncoding(actionContext.Request, "utf-8");
                            Encoding requestEncoding = Encoding.GetEncoding(charset);
                            requestTrace.AppendLine(requestEncoding.GetString(content));
                        } catch {
                            requestTrace.AppendLine("<BINARY DATA>");
                        }
                    }
                    else
                    {
                        requestTrace.AppendLine("<BINARY DATA>");
                    }
                }

                restExposeContext.RequestTrace = requestTrace.ToString();
            }
        }
示例#4
0
        public static void SetupLoggingContext(HttpActionContext actionContext, string serviceName, string ssKey)
        {
            bool restExposeAction         = actionContext.ActionDescriptor.GetCustomAttributes <RESTExposeActionPropertiesAttribute>().Any();
            bool restScreenServicesAction = actionContext.ActionDescriptor.GetCustomAttributes <RESTScreenServicesActionPropertiesAttribute>().Any();

            if (!restExposeAction && !restScreenServicesAction)
            {
                return;
            }

            RESTExposeContext restExposeContext = new RESTExposeContext();

            restExposeContext.StartTime   = DateTime.Now;
            restExposeContext.ServiceName = serviceName;

            if (restExposeAction)   // For REST expose actions
            {
                var configuration = RESTServiceConfiguration.GetCustomClientConfiguration(ssKey, AppInfo.GetAppInfo().eSpaceId);
                restExposeContext.TraceAll    = configuration.TraceAll;
                restExposeContext.TraceErrors = configuration.TraceErrors;
                RESTExposeActionPropertiesAttribute propertiesAttribute = actionContext.ActionDescriptor.GetCustomAttributes <RESTExposeActionPropertiesAttribute>().Single();
                restExposeContext.ActionName       = propertiesAttribute.Name;
                restExposeContext.IsRequestBinary  = propertiesAttribute.IsRequestBinary;
                restExposeContext.IsResponseBinary = propertiesAttribute.IsResponseBinary;
            }
            else     // For Screen Services actions
            {
                var configuration = RESTServiceConfiguration.GetMobileLoggingLevelConfiguration(AppInfo.GetAppInfo().eSpaceId);
                restExposeContext.TraceAll    = configuration.TraceAll;
                restExposeContext.TraceErrors = configuration.TraceErrors;
                RESTScreenServicesActionPropertiesAttribute propertiesAttribute = actionContext.ActionDescriptor.GetCustomAttributes <RESTScreenServicesActionPropertiesAttribute>().Single();
                restExposeContext.ActionName      = propertiesAttribute.Name;
                restExposeContext.ScreenName      = propertiesAttribute.Screen;
                restExposeContext.IsScreenService = true;
            }

            HttpContext.Current.Items[RESTContextVariableName] = restExposeContext;
        }
示例#5
0
        private static bool TrySetRESTContextForServiceAPIMethods(HttpActionContext actionContext, string serviceName, string ssKey, ref RESTExposeContext restExposeContext)
        {
            RESTServiceAPIMethodPropertiesAttribute props = actionContext.ActionDescriptor.GetCustomAttributes <RESTServiceAPIMethodPropertiesAttribute>().SingleOrDefault();

            if (props != null)
            {
                restExposeContext.LogTo = LogTo.ServiceAPIs;

                restExposeContext.TraceAll             = RuntimePlatformSettings.ServiceAPIs.TraceAll.GetValue();
                restExposeContext.TraceErrors          = RuntimePlatformSettings.ServiceAPIs.TraceErrors.GetValue();
                restExposeContext.ServiceAPIMethodName = props.Name;
                restExposeContext.IsRequestBinary      = props.IsRequestBinary;
                restExposeContext.IsResponseBinary     = props.IsResponseBinary;

                return(true);
            }
            return(false);
        }
示例#6
0
        private static bool TrySetRESTContextForScreenServicesActions(HttpActionContext actionContext, ref RESTExposeContext restExposeContext)
        {
            RESTScreenServicesActionPropertiesAttribute props = actionContext.ActionDescriptor.GetCustomAttributes <RESTScreenServicesActionPropertiesAttribute>().SingleOrDefault();

            if (props != null)
            {
                restExposeContext.LogTo = LogTo.ScreenServices;

                restExposeContext.TraceAll    = RuntimePlatformSettings.ScreenServices.TraceAll.GetValue();
                restExposeContext.TraceErrors = RuntimePlatformSettings.ScreenServices.TraceErrors.GetValue();
                restExposeContext.ActionName  = props.Name;
                restExposeContext.ScreenName  = props.Screen;

                return(true);
            }
            return(false);
        }
示例#7
0
        private static bool TrySetRESTContextForRESTExposeActions(HttpActionContext actionContext, string ssKey, ref RESTExposeContext restExposeContext)
        {
            RESTExposeActionPropertiesAttribute props = actionContext.ActionDescriptor.GetCustomAttributes <RESTExposeActionPropertiesAttribute>().SingleOrDefault();

            if (props != null)
            {
                restExposeContext.LogTo = LogTo.RESTExpose;

                var moduleKey = AppInfo.GetAppInfo().eSpaceUID;
                restExposeContext.TraceAll         = RESTSettings.GetTraceAllSetting(moduleKey, ssKey).GetValue();
                restExposeContext.TraceErrors      = RESTSettings.GetTraceErrorsSetting(moduleKey, ssKey).GetValue();
                restExposeContext.ActionName       = props.Name;
                restExposeContext.IsRequestBinary  = props.IsRequestBinary;
                restExposeContext.IsResponseBinary = props.IsResponseBinary;

                return(true);
            }
            return(false);
        }
示例#8
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;
            }
        }
示例#9
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;
            }
        }