public virtual void ScriptFinished(ScriptFinishedResult result, string error = null) { if (ChildScriptFailed && !ChildScriptErrorCaught) { error = "Terminate with failure"; result = ScriptFinishedResult.Error; Log.Fatal("Result Code: " + result.ToString()); } else { Log.Information("Result Code: " + result.ToString()); } //add result variable if missing var resultVar = EngineContext.Variables.Where(f => f.VariableName == "OpenBots.Result").FirstOrDefault(); //handle if variable is missing if (resultVar == null) { resultVar = new OBScriptVariable() { VariableName = "OpenBots.Result", VariableValue = "" } } ; //check value var resultValue = resultVar.VariableValue.ToString(); if (error == null) { Log.Information("Error: None"); if (string.IsNullOrEmpty(resultValue)) { EngineContext.TaskResult = "Successfully Completed Script"; } else { EngineContext.TaskResult = resultValue; } } else { if (ErrorsOccured.Count > 0) { error = ErrorsOccured.OrderByDescending(x => x.LineNumber).FirstOrDefault().StackTrace; } Log.Error("Error: " + error); EngineContext.TaskResult = error; } if ((EngineContext.ScriptEngine != null && !EngineContext.IsChildEngine) || (EngineContext.IsServerExecution && !EngineContext.IsServerChildExecution)) { Log.CloseAndFlush(); } EngineContext.CurrentEngineStatus = EngineStatus.Finished; ScriptFinishedEventArgs args = new ScriptFinishedEventArgs { LoggedOn = DateTime.Now, Result = result, Error = error, ExecutionTime = _stopWatch.Elapsed, FileName = EngineContext.FilePath }; //convert to json var serializedArguments = JsonConvert.SerializeObject(args); //write execution metrics if (EngineContext.EngineSettings.TrackExecutionMetrics && (EngineContext.FilePath != null)) { string summaryLoggerFilePath = Path.Combine(Folders.GetFolder(FolderType.LogFolder), "OpenBots Execution Summary Logs.txt"); Logger summaryLogger = new LoggingMethods().CreateJsonFileLogger(summaryLoggerFilePath, RollingInterval.Infinite); summaryLogger.Information(serializedArguments); if (!EngineContext.IsChildEngine) { summaryLogger.Dispose(); } } ScriptFinishedEvent?.Invoke(this, args); }
public async override Tasks.Task RunCommand(object sender) { var engine = (IAutomationEngineInstance)sender; //get text to log and log file name var textToLog = (string)await v_LogText.EvaluateCode(engine); var loggerFilePath = (string)await v_LogFile.EvaluateCode(engine); LogEventLevel logLevel = LogEventLevel.Information; //determine log file switch (v_LogType) { case "Verbose": logLevel = LogEventLevel.Verbose; break; case "Debug": logLevel = LogEventLevel.Debug; break; case "Information": logLevel = LogEventLevel.Information; break; case "Warning": logLevel = LogEventLevel.Warning; break; case "Error": logLevel = LogEventLevel.Error; break; case "Fatal": logLevel = LogEventLevel.Fatal; break; } if (loggerFilePath != "Engine Logs") { //create new logger and log to custom file using (var logger = new LoggingMethods().CreateFileLogger(loggerFilePath, RollingInterval.Infinite)) { switch (v_LogType) { case "Verbose": logger.Verbose(textToLog); break; case "Debug": logger.Debug(textToLog); break; case "Information": logger.Information(textToLog); break; case "Warning": logger.Warning(textToLog); break; case "Error": logger.Error(textToLog); break; case "Fatal": logger.Fatal(textToLog); break; } } } string logMessage = $"{v_LogType} - {textToLog}"; engine.ReportProgress($"Logging Line {LineNumber}: {(v_IsPrivate ? engine.PrivateCommandLog : logMessage)}", Enum.GetName(typeof(LogEventLevel), logLevel)); }