Esempio n. 1
0
        /// <summary>
        /// Log every command run by a users.
        /// </summary>
        /// <param name="consoleCommand"></param>
        /// <param name="cmdTypeToRun"></param>
        /// <param name="command"></param>
        /// <param name="startTime"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        private HttpResponseMessage AddLogAndReturnResponse(IConsoleCommand consoleCommand, Type cmdTypeToRun, CommandInputModel command,
                                                            DateTime startTime, string error = null)
        {
            HttpResponseMessage message;
            var isValid = consoleCommand?.IsValid() ?? false;
            var logInfo = new LogInfo
            {
                LogTypeKey = "PROMPT_ALERT"
            };

            logInfo.LogProperties.Add(new LogDetailInfo("Command", FilterCommand(command.CmdLine)));
            logInfo.LogProperties.Add(new LogDetailInfo("IsValid", isValid.ToString()));

            try
            {
                if (cmdTypeToRun != null)
                {
                    logInfo.LogProperties.Add(new LogDetailInfo("TypeFullName", cmdTypeToRun.FullName));
                }
                if (isValid)
                {
                    var result = consoleCommand.Run();
                    if (result.PagingInfo != null)
                    {
                        if (result.PagingInfo.PageNo < result.PagingInfo.TotalPages)
                        {
                            result.Output = string.Format(Localization.GetString("Prompt_PagingMessageWithLoad", Constants.LocalResourcesFile),
                                                          result.PagingInfo.PageNo, result.PagingInfo.TotalPages);

                            var args        = command.Args;
                            var indexOfPage = args.Any(x => x.ToLowerInvariant() == "--page")
                                ? args.TakeWhile(arg => arg.ToLowerInvariant() != "--page").Count()
                                : -1;
                            if (indexOfPage > -1)
                            {
                                args[indexOfPage + 1] = (result.PagingInfo.PageNo + 1).ToString();
                            }
                            var nextPageCommand = string.Join(" ", args);
                            if (indexOfPage == -1)
                            {
                                nextPageCommand += " --page " + (result.PagingInfo.PageNo + 1);
                            }
                            result.NextPageCommand = nextPageCommand;
                        }
                        else if (result.Records > 0)
                        {
                            result.Output = string.Format(Localization.GetString("Prompt_PagingMessage", Constants.LocalResourcesFile),
                                                          result.PagingInfo.PageNo, result.PagingInfo.TotalPages);
                        }
                    }
                    message = this.Request.CreateResponse(HttpStatusCode.OK, result);
                    logInfo.LogProperties.Add(new LogDetailInfo("RecordsAffected", result.Records.ToString()));
                    logInfo.LogProperties.Add(new LogDetailInfo("Output", result.Output));
                }
                else
                {
                    logInfo.LogProperties.Add(new LogDetailInfo("Output", consoleCommand?.ValidationMessage ?? error));
                    message = this.BadRequestResponse(consoleCommand?.ValidationMessage ?? error);
                }
            }
            catch (Exception ex)
            {
                logInfo.Exception = new ExceptionInfo(ex);
                message           = this.BadRequestResponse(ex.Message);
            }
            logInfo.LogProperties.Add(new LogDetailInfo("ExecutionTime(hh:mm:ss)", TimeSpan.FromMilliseconds(DateTime.Now.Subtract(startTime).TotalMilliseconds).ToString(@"hh\:mm\:ss\.ffffff")));
            LogController.Instance.AddLog(logInfo);
            return(message);
        }