public static void OpenCurrentLogFile([NotNull] Instance instance, [NotNull] Window owner, [CanBeNull] string logFileType = null)
        {
            Assert.ArgumentNotNull(instance, "instance");
            Assert.ArgumentNotNull(owner, "owner");

            string dataFolderPath = instance.DataFolderPath;

            FileSystem.FileSystem.Local.Directory.AssertExists(dataFolderPath, "The data folder ({0}) of the {1} instance doesn't exist".FormatWith(dataFolderPath, instance.Name));
            var logs = instance.LogsFolderPath;

            logFileType = logFileType ?? GetLogFileTypes(owner, logs);
            if (logFileType == null)
            {
                return;
            }

            var pattern     = logFileType + "*.txt";
            var files       = FileSystem.FileSystem.Local.Directory.GetFiles(logs, pattern) ?? new string[0];
            var logFilePath = files.OrderByDescending(FileSystem.FileSystem.Local.File.GetCreationTimeUtc).FirstOrDefault();

            if (string.IsNullOrEmpty(logFilePath))
            {
                return;
            }

            string logviewer = AppSettings.AppToolsLogViewer.Value;

            if (string.IsNullOrEmpty(logviewer))
            {
                return;
            }

            if (logviewer == "logview.exe")
            {
                logviewer = ApplicationManager.GetEmbeddedApp("logview.zip", "SIM.Tool.Windows", "logview.exe");
            }

            var fileSystemWatcher = new FileSystemWatcher(logs)
            {
                Filter = pattern,
                IncludeSubdirectories = false
            };

            bool ignore = false;

            var process = WindowHelper.RunApp(logviewer, logFilePath);

            if (process == null)
            {
                return;
            }

            // we need to stop all this magic when application closes
            process.Exited += (sender, args) =>
            {
                // but shouldn't if it is initiated by this magic
                if (ignore)
                {
                    ignore = false;
                    return;
                }

                fileSystemWatcher.EnableRaisingEvents = false;
            };

            fileSystemWatcher.Created += (sender, args) =>
            {
                try
                {
                    // indicate that magic begins
                    ignore = true;

                    // magic begins
                    process.Kill();
                    files       = FileSystem.FileSystem.Local.Directory.GetFiles(logs, pattern) ?? new string[0];
                    logFilePath = files.OrderByDescending(FileSystem.FileSystem.Local.File.GetCreationTimeUtc).First();
                    process     = WindowHelper.RunApp(logviewer, logFilePath);
                }
                catch (Exception ex)
                {
                    Log.Error("Unhandled error happened while reopening log file", typeof(InstanceHelperEx), ex);
                }
                finally
                {
                    fileSystemWatcher.EnableRaisingEvents = false;
                }
            };

            fileSystemWatcher.EnableRaisingEvents = true;
        }
        public static void OpenCurrentLogFile([NotNull] Instance instance, [NotNull] Window owner, [CanBeNull] string logFileType = null)
        {
            Assert.ArgumentNotNull(instance, "instance");
            Assert.ArgumentNotNull(owner, "owner");

            var dataFolderPath = instance.DataFolderPath;

            FileSystem.FileSystem.Local.Directory.AssertExists(dataFolderPath, "The data folder ({0}) of the {1} instance doesn't exist".FormatWith(dataFolderPath, instance.Name));

            var logsFolderPath = instance.LogsFolderPath;
            var logFilePrefix  = logFileType ?? GetLogFileTypes(owner, logsFolderPath);

            if (logFilePrefix == null)
            {
                Action waitForLogs = delegate
                {
                    while (logFilePrefix == null)
                    {
                        logFilePrefix = GetLogFileTypes(owner, logsFolderPath);
                        Thread.Sleep(100);
                    }
                };

                WindowHelper.LongRunningTask(waitForLogs, "Waiting for log files", owner, null, "Waiting for log files to be created in the \"{0}\" folder.".FormatWith(logsFolderPath));
            }

            var logFilePattern = logFilePrefix + "*.txt";
            var files          = FileSystem.FileSystem.Local.Directory.GetFiles(logsFolderPath, logFilePattern) ?? new string[0];
            var logFilePath    = files.OrderByDescending(FileSystem.FileSystem.Local.File.GetCreationTimeUtc).FirstOrDefault();

            if (string.IsNullOrEmpty(logFilePath))
            {
                Action waitForLogs = delegate
                {
                    while (string.IsNullOrEmpty(logFilePath))
                    {
                        var files2 = FileSystem.FileSystem.Local.Directory.GetFiles(logsFolderPath, logFilePattern) ?? new string[0];
                        logFilePath = files2.OrderByDescending(FileSystem.FileSystem.Local.File.GetCreationTimeUtc).FirstOrDefault();
                        Thread.Sleep(100);
                    }
                };

                WindowHelper.LongRunningTask(waitForLogs, "Waiting for log files", owner, null, "Waiting for log files to be created in the \"{0}\" folder.".FormatWith(logsFolderPath));
            }

            var logViewer = GetLogViewer();

            if (string.IsNullOrEmpty(logViewer))
            {
                return;
            }

            var fileSystemWatcher = new FileSystemWatcher(logsFolderPath)
            {
                Filter = logFilePattern,
                IncludeSubdirectories = false
            };

            var reopenLogViewer = false;

            var currentProcess = WindowHelper.RunApp(logViewer, logFilePath);

            if (currentProcess == null)
            {
                return;
            }

            // we need to stop all this magic when application closes
            currentProcess.Exited += delegate
            {
                // but shouldn't if it is initiated by this magic
                if (reopenLogViewer)
                {
                    reopenLogViewer = false;
                    return;
                }

                fileSystemWatcher.EnableRaisingEvents = false;
            };

            fileSystemWatcher.Created += (sender, args) =>
            {
                try
                {
                    if (args.ChangeType != WatcherChangeTypes.Created)
                    {
                        return;
                    }

                    var filePath = args.FullPath;
                    if (!filePath.Contains(logFilePrefix))
                    {
                        return;
                    }

                    // indicate that magic begins
                    reopenLogViewer = true;

                    // magic begins
                    currentProcess.Kill();

                    currentProcess = WindowHelper.RunApp(logViewer, filePath);

                    // we need to stop all this magic when application closes
                    currentProcess.Exited += delegate
                    {
                        // but shouldn't if it is initiated by this magic
                        if (reopenLogViewer)
                        {
                            reopenLogViewer = false;
                            return;
                        }

                        fileSystemWatcher.EnableRaisingEvents = false;
                    };
                }
                catch (Exception ex)
                {
                    fileSystemWatcher.EnableRaisingEvents = false;
                    Log.Error(ex, "Unhandled error happened while reopening log file");
                }
            };

            fileSystemWatcher.EnableRaisingEvents = true;
        }
        public static bool PreheatInstance(Instance instance, Window mainWindow, bool ignoreAdvancedSetting = false)
        {
            if (!EnsureAppPoolState(instance, mainWindow))
            {
                return(false);
            }

            if (!AppSettings.AppPreheatEnabled.Value && !ignoreAdvancedSetting)
            {
                return(true);
            }

            // Check if the instance is responsive now
            if (!InstanceHelper.IsInstanceResponsive(instance, "fast"))
            {
                // It is not responsive so we need to preheat it
                // i.e. request with larger timeout and with the
                // progress bar shown to the user to avoid UI lag
                Exception ex  = null;
                var       res = WindowHelper.LongRunningTask(() => PreheatInstance(instance, out ex), "Starting Sitecore", mainWindow,
                                                             "Sitecore is being initialized",
                                                             "It may take up to a few minutes on large solutions or slow machines.",
                                                             true, true, true);
                if (res == null)
                {
                    return(false);
                }

                // if error happened
                if (ex != null)
                {
                    const string cancel          = "Cancel";
                    const string openLog         = "Open SIM log file";
                    const string openSitecoreLog = "Open Sitecore log file";
                    const string openAnyway      = "Open in browser";
                    var          message         = "The instance returned an error. \n\n" + ex.Message;
                    Log.Error(message, typeof(WindowHelper), ex);
                    var result = WindowHelper.AskForSelection("Running instance failed", null, message,
                                                              new[]
                    {
                        cancel, openLog, openSitecoreLog, openAnyway
                    }, mainWindow);
                    switch (result)
                    {
                    case openLog:
                        WindowHelper.OpenFile(Log.LogFilePath);
                        return(false);

                    case openSitecoreLog:
                        OpenCurrentLogFile(instance, mainWindow);
                        return(false);

                    case openAnyway:
                        return(true);

                    default:
                        return(false);
                    }
                }
            }

            return(true);
        }
 public static void OpenFile(string path)
 {
     WindowHelper.RunApp("explorer.exe", path.Replace('/', '\\'));
 }