public bool DoImport()

in ReadTraceNexusImporter/ReadTraceNexusImporter.cs [398:492]


        public bool DoImport()
        {
            string firstTrcFile = "";
            Util.Logger.LogMessage("ReadTraceNexusImporter - Starting import...");
            string[] files = Directory.GetFiles(Path.GetDirectoryName(this.traceFileSpec), Path.GetFileName(this.traceFileSpec));

            if (null == this.readTracePath)
                throw new Exception("Cannot locate ReadTrace.exe. This import requires ReadTrace version 9.0.9.0 or later.");
            if (0 == files.Length)
            {
                State = ImportState.NoFiles;
                return false;
            }

            State = ImportState.Importing;

            // Find the first trace file. 
            firstTrcFile = FindFirstTraceFile(files);


            // -T18 means to disable pop up of reporter.exe at end of load 
            // -T35 to enable mars
            string args = String.Format("-S\"{0}\" \"-d{1}\" {2} -T18 -T28 -T29 \"-I{3}\" {4} {5} {6} \"-o{7}\" {8} {9} {10}",
                this.sqlServer,                                                 // -S{0}                            SQL Server name 
                this.database,                                                  // -d{1}                            Database name
                (this.useWindowsAuth ? "-E" : String.Format("-U\"{0}\" -P\"{1}\"", this.sqlLogin, this.sqlPassword)), // {2} = -E (or) -Uuser -PPassword  Credentials
                firstTrcFile,                                                   // -I{3}                            Profiler trace file
                ((bool)this.options[OPTION_OUTPUT_RML] ? "" : "-f"),            // {4} = -f                         Optional: enable RML file generation
                ((bool)this.options[OPTION_OUTPUT_SPID_TRC] ? "-M" : ""),       // {5} = -M                         Optional: output spid-specific .trc files
                ((bool)this.options[OPTION_QUOTED_IDENTIFIERS] ? "" : "-Q"),    // {6} = -Q                         Optional: assume quoted identifiers OFF
                Path.GetTempPath() + "RML",     // -o{7}  Temp output path (%TEMP%\RML)
                ((bool)this.options[OPTION_IGNORE_PSSDIAG_HOST] ? "-H\"!PSSDIAG\"" : ""),   //  {8}   Using 9.00.009 ReadTrace ignore events with HOST=PSSDIAG 
                ((bool)this.options[OPTION_DISABLE_EVENT_REQUIREMENTS] ? "-T28 -T29 " : ""),       //  {9} tell ReadTrace to override event requirement checks 
                ((bool)this.options[OPTION_ENABLE_MARS] ? "-T35":"")
            );

            Util.Env["RMLLogDir"] = Path.GetTempPath() + "RML";
            
            Util.Env.ReadTraceLogFile = Path.GetTempPath() + @"RML\readtrace.log";
            Util.Logger.LogMessage("ReadTraceNexusImporter: Loading " + firstTrcFile);
            Util.Logger.LogMessage("ReadTraceNexusImporter: Temp Path: " + Path.GetTempPath());

#if DEBUG
            Util.Logger.LogMessage("ReadTraceNexusImporter (DEBUG ONLY BEFORE): Cmd Line: " + this.readTracePath + " " + args);
#endif

            //      Don't log clear text password 
            string argsOut = args;
            if (false == this.useWindowsAuth)
            {
                argsOut = args.Replace("\"" + this.sqlPassword + "\"", "\"********\"");
            }

            Util.Logger.LogMessage("ReadTraceNexusImporter: Cmd Line: " + this.readTracePath + " " + argsOut);

            ProcessStartInfo pi = new ProcessStartInfo(this.readTracePath, args);
            pi.CreateNoWindow = false;
            pi.UseShellExecute = false;

            // NOTE: If we decide to redirect in the future we have to read from the pipe or when
            //       the pipe is full ReadTrace will hang attempting to write to stdout

            pi.RedirectStandardOutput = false;  // don't redirect -- we can always use the log file at %TEMP%\RML for tshooting
            pi.RedirectStandardError = false;
            processReadTrace = Process.Start(pi);

            int i = 0;
            long rowsInserted = 0;
            while (!processReadTrace.HasExited)
            {
                i++;
                System.Threading.Thread.Sleep(100);
                // We don't have any good way to communicate with readtrace.exe and determine actual number of events processed 
                // or other progress indicators.  As a partial solution, try to retrieve approximate inserted row counts every 2 
                // seconds while we're waiting for readtrace to complete its trace processing. 
                if (0 == i % 20)
                    rowsInserted = GetApproximateTotalRowsInserted();
                this.TotalLinesProcessed = rowsInserted;
                this.TotalRowsInserted = rowsInserted;
                if (canceled)
                    break;
            }

            //LogMessage("ReadTrace stdout: " + processReadTrace.StandardOutput);
            //LogMessage("ReadTrace stderr: " + processReadTrace.StandardError);

            Util.Logger.LogMessage("ReadTrace exitcode: " + processReadTrace.ExitCode.ToString());

            State = ImportState.Idle;

            if (0 == processReadTrace.ExitCode)
                return true;
            else
                return false;
        }