예제 #1
0
 /// <summary>
 /// Marks particular string (like password) as sensitive so that whenever it appears in log it appears masked.
 /// </summary>
 /// <param name="log"></param>
 /// <param name="sensitiveString">Sensitive information like password.</param>
 /// <param name="replacementType">ReplaceWhole = 0,Replace50PercentInMiddle = 1,ReplaceWithLengthInformation = 2</param>
 /// <param name="replacementString">Replacement mask like XXXX.</param>
 public static void AssignSensitiveString(this CustomTraceLog log, string sensitiveString, int replacementType = (int)SensitiveStringReplacementType.Replace50PercentInMiddle, string replacementString = "...hidden...")
 {
     if (log != null)
     {
         log._AssignSensitiveString(sensitiveString, replacementType, replacementString);
     }
 }
예제 #2
0
 public static void AddLineAndDecreaseIdent(this CustomTraceLog log, string message, bool inNewLine = true, int level = 0)
 {
     if (log != null)
     {
         log._AddLineAndDecreaseIdent(message, inNewLine, level);
     }
 }
예제 #3
0
 public static void DecreaseIdent(this CustomTraceLog log)
 {
     if (log != null)
     {
         log._DecreaseIdent();
     }
 }
예제 #4
0
 public static void AddException(this CustomTraceLog log, Exception e, string additionalMessage = null)
 {
     if (log != null)
     {
         log._AddLine("============================= Exception ===========================");
         if (additionalMessage != null)
         {
             log._AddLine(additionalMessage);
             log._AddLine("-------------------------------------------------------------------");
         }
         e = BuildingBlocks.Common.Misc.GetDeepestException(e);//this is usually most interesting part
         log._AddLine(e.Message);
         log._AddLine(e.StackTrace);
         log._AddLine("===================================================================");
     }
 }
예제 #5
0
        public static CustomTraceLog Unbox(object customTraceLog)
        {
            CustomTraceLog log = null;

            if (customTraceLog != null)
            {
                if (!(customTraceLog is CustomTraceLog))
                {
                    throw new Exception("customTraceLog param must be of type CustomTraceLog.");
                }
                else
                {
                    log = (CustomTraceLog)customTraceLog;
                }
            }
            return(log);
        }
예제 #6
0
        public LogScope(CustomTraceLog log, string startMessage, string finishMessage = "done.", int level = 0)
        {
            if (log != null)
            {
                this._log           = log;
                this._startMessage  = startMessage;
                this._finishMessage = finishMessage;
                this._level         = level;

                log.AddLineAndIncreaseIdent(this._startMessage, true, this._level);                 //AddLineAndIncreaseIdent called here

                this._totalLengthAfterStartMessage = log.Length;
            }
            else
            {
                this._disposed = true;
            }
        }
예제 #7
0
        /// <summary>
        /// Pass null for parameters if none exist.
        /// </summary>
        /// <param name="command"></param>
        /// <param name="parameters"></param>
        /// <param name="timeoutInMilliseconds"></param>
        /// <param name="workingFolder"></param>
        /// <param name="log"></param>
        /// <param name="logCommandAndParametersBeforeExecution"></param>
        /// <param name="disableUnhandledExceptionDialog"></param>
        /// <returns></returns>
        public static int ExecuteCommand(string command, string parameters, int timeoutInMilliseconds, string workingFolder = null, CBB_Logging.CustomTraceLog log = null, bool logCommandAndParametersBeforeExecution = true, bool disableUnhandledExceptionDialog = true, string outputPartThatWillForceExitCode0 = null, string outputPartThatWillForceExitCodeMinus1 = null)
        {
            if (log == null)
            {
                log = new CBB_Logging.CustomTraceLog();
            }
            CBB_Logging.CustomTraceLog outputLog = new CBB_Logging.CustomTraceLog();

            if (logCommandAndParametersBeforeExecution)
            {
                CBB_Logging.CustomTraceLogExtensions.AddLineAndIncreaseIdent(log, "Command: " + command + " " + parameters.ToNonNullString());
            }
            else
            {
                CBB_Logging.CustomTraceLogExtensions.AddLineAndIncreaseIdent(log, "Command execution preparation...");
            }

            bool?oldStateOfUnhandledExceptionDialog = null;

            if (disableUnhandledExceptionDialog)
            {
                oldStateOfUnhandledExceptionDialog = GetUnhandledExceptionDialogEnabledState();
                //log.AddLine("State of UnhandledExceptionDialog (DontShowUI in registry):"+(oldStateOfUnhandledExceptionDialog.HasValue?oldStateOfUnhandledExceptionDialog.ToString():"null"));
                //log.AddLine("State of UnhandledExceptionDialog (DontShowUI in registry) will be set to true temporarly...");
                SetUnhandledExceptionDialogEnabledState(true);
                //log.AddLine("State of UnhandledExceptionDialog (DontShowUI in registry) changed.");
            }

            System.Diagnostics.Process process = new System.Diagnostics.Process();
            try
            {
                if (string.IsNullOrEmpty(workingFolder))
                {
                    workingFolder = CraftSynth.BuildingBlocks.Common.Misc.ApplicationRootFolderPath;
                }

                process.StartInfo.WorkingDirectory = workingFolder;
                process.StartInfo.UseShellExecute  = false;
                //process.StartInfo.FileName = @"C:\Windows\System32\cmd.exe";
                process.StartInfo.FileName = Path.Combine(workingFolder, command);
                //process.StartInfo.Arguments = "/C "+ command+" "+parameters;//m_gpg.HomePath + "\\gpg2.exe --import " + Path.Combine(CraftSynth.BuildingBlocks.Common.Misc.ApplicationRootFolderPath, "interelate.asc");
                if (!string.IsNullOrEmpty(parameters))
                {
                    process.StartInfo.Arguments = parameters;
                }
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.ErrorDialog    = false;
                //si.StartInfo.RedirectStandardInput = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError  = true;

                StringBuilder output = new StringBuilder();
                StringBuilder error  = new StringBuilder();
                using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
                    using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
                    {
                        process.OutputDataReceived += (sender, e) =>
                        {
                            if (e.Data == null)
                            {
                                outputWaitHandle.Set();
                            }
                            else
                            {
                                CBB_Logging.CustomTraceLogExtensions.AddLine(log, ">> " + e.Data);
                                CBB_Logging.CustomTraceLogExtensions.AddLine(outputLog, e.Data);
                                output.AppendLine(e.Data);
                            }
                        };
                        process.ErrorDataReceived += (sender, e) =>
                        {
                            if (e.Data == null)
                            {
                                errorWaitHandle.Set();
                            }
                            else
                            {
                                CBB_Logging.CustomTraceLogExtensions.AddLine(log, ">> " + e.Data);
                                CBB_Logging.CustomTraceLogExtensions.AddLine(outputLog, e.Data);
                                error.AppendLine(e.Data);
                            }
                        };

                        CBB_Logging.CustomTraceLogExtensions.AddLine(log, "Command executing...");
                        process.Start();

                        process.BeginOutputReadLine();
                        process.BeginErrorReadLine();

                        if (process.WaitForExit(timeoutInMilliseconds) &&
                            outputWaitHandle.WaitOne(timeoutInMilliseconds) &&
                            errorWaitHandle.WaitOne(timeoutInMilliseconds))
                        {
                            // Process completed. Check process.ExitCode here.
                            //log.AddLine("Done.");
                        }
                        else
                        {
                            // Timed out.
                            CBB_Logging.CustomTraceLogExtensions.AddLine(log, string.Format("Operation timed out. (timeout={0})", timeoutInMilliseconds));
                        }
                    }
            }
            finally
            {
                if (disableUnhandledExceptionDialog)
                {
                    //log.AddLine("Recovering state of UnhandledExceptionDialog (DontShowUI in registry)...");
                    SetUnhandledExceptionDialogEnabledState(oldStateOfUnhandledExceptionDialog);
                    //log.AddLine("Recovered.");
                }
            }


            CBB_Logging.CustomTraceLogExtensions.AddLine(log, "ExitCode: " + process.ExitCode);
            int r = process.ExitCode;

            if (!string.IsNullOrEmpty(outputPartThatWillForceExitCode0) || !string.IsNullOrEmpty(outputPartThatWillForceExitCodeMinus1))
            {
                List <string> outputLogLines = outputLog.ToString().Split('\n').ToList();

                //chack only part of log that represent output for success and failure indicators
                if (outputLogLines.Count > 0)
                {
                    for (int i = outputLogLines.Count - 1; i >= 0; i--)
                    {
                        if (!string.IsNullOrEmpty(outputPartThatWillForceExitCode0) && outputLogLines[i].Contains(outputPartThatWillForceExitCode0))
                        {
                            CBB_Logging.CustomTraceLogExtensions.AddLine(log, "Setting ExitCode to 0 because this indicator was found in output:" + outputPartThatWillForceExitCode0);
                            r = 0;
                            break;
                        }

                        if (!string.IsNullOrEmpty(outputPartThatWillForceExitCodeMinus1) && outputLogLines[i].Contains(outputPartThatWillForceExitCodeMinus1))
                        {
                            CBB_Logging.CustomTraceLogExtensions.AddLine(log, "Setting ExitCode to -1 because this indicator was found in output:" + outputPartThatWillForceExitCodeMinus1);
                            r = -1;
                            break;
                        }
                    }
                }
            }

            CBB_Logging.CustomTraceLogExtensions.DecreaseIdent(log);
            return(r);
        }
예제 #8
0
 public static LogScope LogScope(this CustomTraceLog log, string startMessage, string finishMessage = "done.", int level = 0)
 {
     return(new LogScope(log, startMessage, finishMessage, level));
 }