Ejemplo n.º 1
0
        public virtual IHttpActionResult Log(Payload.LogMessage[] logs, long clientTimeInMillis)
        {
            AppInfo   appInfo  = null;
            HeContext context  = null;
            int       espaceId = 0;
            int       tenantId = 0;
            string    sessionId;
            int       userId = 0;

            try {
                appInfo = AppInfo.GetAppInfo();
                if (!IsApplicationEnabled(appInfo))
                {
                    return(GetErrorResponseResult("Application Backend Unavailable", HttpStatusCode.ServiceUnavailable));
                }

                TimeSpan dateDiff = TimeSpan.Zero;
                if (clientTimeInMillis > 0)
                {
                    dateDiff = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(clientTimeInMillis).Subtract(DateTime.UtcNow);
                }

                ValidateRequestSecurity();

                GatherLogInformation(/*updateSessionFromCookie*/ true, appInfo, out context, out espaceId, out tenantId, out sessionId, out userId);
                var allowAuditing      = appInfo == null /*because of tests */ || appInfo.Properties.AllowAuditing;
                var clientTraceEnabled = appInfo == null /*because of tests */ || appInfo.Properties.ClientSideTracingEnabled;

                if (logs.Length > maxLogsPerBatch)
                {
                    return(GetErrorResponseResult("Too many logs in a batch.", HttpStatusCode.ServiceUnavailable));
                }

                if (!CanProcessLogRequest(logs, allowAuditing, clientTraceEnabled))
                {
                    return(GetErrorResponseResult("Too many requests", HttpStatusCode.ServiceUnavailable));
                }

                foreach (var log in logs)
                {
                    string logType = log.LogType;

                    DateTime date = DateTime.Parse(log.Instant);
                    if (clientTimeInMillis > 0)
                    {
                        date = Conversions.DateTimeToUniversal(date).Subtract(dateDiff);
                    }
                    date = Conversions.DateTimeToLocal(date);

                    if (StringUtils.EqualsIgnoreCase(logType, "general"))
                    {
                        if (allowAuditing)
                        {
                            WriteToGeneralLog(log, date, sessionId, espaceId, tenantId, userId);
                        }
                    }
                    else if (StringUtils.EqualsIgnoreCase(logType, "trace"))
                    {
                        if (clientTraceEnabled)
                        {
                            OSTrace.Debug("[" + date.ToString("yyyy-MM-dd HH:mm:ss.ffff") + "] " + log.Module_Name + " - " + log.Message);
                        }
                    }
                    else if (StringUtils.EqualsIgnoreCase(logType, "error"))
                    {
                        string environmentInformation = ErrorLog.GetStackEnvironmentInfo(appInfo, context);
                        environmentInformation += "\nClient-Side Log";
                        if (log.ErrorStack != null)   // Limit stack to the lower storage size
                        {
                            log.ErrorStack = RuntimePlatformUtils.Trunc(log.ErrorStack, ErrorLog.MAX_STACK_SMALL_STORAGE_SIZE);
                        }
                        ErrorLog.StaticWrite(date, sessionId, espaceId, tenantId, userId, log.Message, log.ErrorStack, environmentInformation, log.Module_Name);
                    }
                    else
                    {
                        ErrorLog.LogApplicationError("Invalid log type: " + logType, /*stack*/ null, context, "Module Services");
                        return(GetErrorResponseResult("Invalid log type", HttpStatusCode.InternalServerError));
                    }
                }
            } catch (Exception ex) {
                DatabaseAccess.FreeupResources(false);

                var licensingException = ex as LicensingException;
                if (licensingException != null)
                {
                    // Error was already logged inside AppInfo's GetAppInfo
                    return(GetErrorResponseResult("Application Backend Unavailable", HttpStatusCode.ServiceUnavailable));
                }

                var exposeRestException = ex as ExposeRestException;
                if (exposeRestException != null)
                {
                    ErrorLog.LogApplicationError(exposeRestException, appInfo, context, "Module Services");
                    return(GetErrorResponseResult(exposeRestException.Message, exposeRestException.StatusCode));
                }

                ErrorLog.LogApplicationError(ex, appInfo, context, "Module Services");
                return(GetErrorResponseResult("Internal Server Error", HttpStatusCode.InternalServerError));
            }

            return(Ok());
        }