Exemplo n.º 1
0
        public static LogElementResponse LogElement(LogElementDTO logElement)
        {
            lock (logElementLock)
            {
                try
                {
                    var config = ConfigurationHelper.GetConfigurationSection();
                    if (!config.Enabled)
                    {
                        return new LogElementResponse()
                               {
                                   Success = true
                               }
                    }
                    ;

                    var unixNow = TimeHelper.UnixTimestamp();

                    LogElementDTO lastLogElement = null;
                    //Cleanup old entries in dictionary
                    dictLastLogElementPerPage.Where(x => x.Value.UnixTimestamp < unixNow - cleanupAfterLastLogElementsAfterSeconds).Select(x => x.Key).ToList().ForEach(x => dictLastLogElementPerPage.Remove(x));

                    if (dictLastLogElementPerPage.TryGetValue(logElement.PageGUID, out lastLogElement))
                    {
                        if (lastLogElement.UnixTimestamp >= logElement.UnixTimestamp)
                        {
                            //Correct logElement, it must be placed after lastLogElement
                            var diffAdjust = (lastLogElement.UnixTimestamp + unixTimestampForwardAdjuster) - logElement.UnixTimestamp;
                            logElement.UnixTimestamp += diffAdjust;

                            if (logElement.UnixTimestampEnd.HasValue)
                            {
                                logElement.UnixTimestampEnd += diffAdjust;
                            }
                        }
                    }
                    else
                    {
                        //This is the first registered logElement for this pageGUID, nothing to correct against
                    }

                    if (unixNow < logElement.UnixTimestamp) //Get server back on right track if the logElement.UnixTimestamp is ahead of time, which it could be if the logElement was transfered from e.g. the clientside(browser)
                    {
                        var diffSeconds = logElement.UnixTimestamp - unixNow;

                        var diffMS = (int)Math.Ceiling(diffSeconds * 1000.0);
                        System.Threading.Thread.Sleep(diffMS);
                    }

                    dictLastLogElementPerPage[logElement.PageGUID] = logElement;

                    GetLoggingPersister(config.LogType).LogElement(config.FilePath, logElement);

                    return(new LogElementResponse()
                    {
                        Success = true
                    });
                }
                catch (Exception ex)
                {
                    return(new LogElementResponse()
                    {
                        Success = false, Message = ex.Message + " ::: " + ex.StackTrace
                    });
                }
            }
        }
        public static LogElementResponse LogRequest(SqlCommandLRAP cmd, LoggingDBType type, CommandBehavior behavior = CommandBehavior.Default)
        {
            var context = HttpContext.Current;

            var page        = HttpContext.Current.Handler as Page;
            var logType     = LogType.OnPersistenceRequest;
            var sessionGUID = LoggingHelper.GetSessionGUID(HttpContext.Current, HttpContext.Current.Handler as Page, () => new Guid()).Value;
            var pageGUID    = LoggingHelper.GetPageGUID(HttpContext.Current, HttpContext.Current.Handler as Page, () => new Guid()).Value;

            var cmdDTO = MapSqlCommandLRAPToSqlCommandDTO(cmd, type, behavior);

            var newLogElement = new LogElementDTO(
                guid: Guid.NewGuid(),
                sessionGUID: sessionGUID,
                pageGUID: pageGUID,
                bundleGUID: cmdDTO.BundleGUID,
                progressGUID: null,
                unixTimestamp: TimeHelper.UnixTimestamp(),
                logType: logType,
                element: cmdDTO.CommandText,
                element2: null,
                value: SerializationHelper.Serialize(cmdDTO, SerializationType.Json),
                times: 1,
                unixTimestampEnd: null,
                instanceTime: DateTime.Now,
                stackTrace: ConfigurationHelper.GetConfigurationSection().LogStackTrace ? new StackTrace().ToString() : ""
                );

            if (LoggingHelper.IsPlaying(context, page?.Request.Params))
            {
                var serverGUID = LoggingHelper.GetServerGUID(HttpContext.Current, null, page?.Request.Params).Value;

                if (LoggingHelper.FetchAndExecuteLogElement(serverGUID, sessionGUID, pageGUID, logType, (logElement) =>
                {
                    TimeHelper.SetNow(HttpContext.Current, logElement.InstanceTime);

                    var loggedCmdDTO = SerializationHelper.Deserialize <SqlCommandDTO>(logElement.Value, SerializationType.Json);

                    if (!cmdDTO.Equals(loggedCmdDTO))
                    {
                        //Show UI-warning.. something in the request differs
                    }

                    //Kan jo ikke bare overskrive... uden at spørge brugeren om det er det der ønskes, det kan ihf ikke være default behavior
                    //LoggingHelper.SetRequestValues(context, requestParams.Form, requestForm);

                    //var requestParams = requestForm != null ? WebHelper.ParamsWithSpecialRequestForm(context, requestForm) : context.Request?.Params;

                    PlayerCommunicationHelper.SetLogElementAsDone(serverGUID, sessionGUID, pageGUID, logElement.GUID, new JobStatus()
                    {
                        Success = true
                    });                                                                                                                                  //, async: false);
                }))
                {
                    return new LogElementResponse()
                           {
                               Success = true, Object = cmdDTO
                           }
                }
                ;
            }

            var result = LoggingHelper.LogElement(newLogElement);

            result.Object = cmdDTO;

            return(result);
        }