Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            var options = new Options();

            if (!CommandLine.Parser.Default.ParseArguments(args, options))
            {
                return;
            }

            CollectorLogger logger = new CollectorLogger(options.ServerInstance);

            try
            {
                string     codeBase = Assembly.GetExecutingAssembly().CodeBase;
                UriBuilder uri      = new UriBuilder(codeBase);
                string     path     = Uri.UnescapeDataString(uri.Path);

                String targetProcess = Path.Combine(Path.GetDirectoryName(path), "ExtendedXEReaderCollector.exe");
                String arguments     = "-S " + options.ServerInstance + " -v " + options.Verbose;

                Process.Start(targetProcess, arguments);
            }
            catch (Exception e)
            {
                logger.logMessage(e.StackTrace);
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            var options = new Options();

            if (!CommandLine.Parser.Default.ParseArguments(args, options))
            {
                return;
            }

            bool            verbose = options.Verbose;
            CollectorLogger logger  = null;


            try
            {
                String SourceServerInstance = options.ServerInstance;
                Guid   CollectionSetUid     = new Guid(options.CollectionSetUID);
                int    ItemId = options.CollectionItemID;
                int    LogId  = options.LogId;

                logger = new CollectorLogger(SourceServerInstance, CollectionSetUid, ItemId);

                if (verbose)
                {
                    logger.logMessage("Starting");
                }

                Uploader uploader = new Uploader(SourceServerInstance, CollectionSetUid, ItemId, LogId);
                uploader.verbose = verbose;
                uploader.UploadData();

                if (verbose)
                {
                    logger.logMessage("Ending with success");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                if (verbose)
                {
                    logger.logMessage("Ending with failure");
                }
                if (verbose)
                {
                    logger.logMessage(e.Message);
                }
                if (verbose)
                {
                    logger.logMessage(e.StackTrace.ToString());
                }
            }
        }
        public RestoreSummaryRequest(
            RestoreRequest request,
            string inputPath,
            ISettings settings,
            IReadOnlyList <SourceRepository> sources)
        {
            Request   = request;
            Settings  = settings;
            Sources   = sources;
            InputPath = inputPath;

            CollectorLogger = new CollectorLogger(request.Log);
            request.Log     = CollectorLogger;
        }
Ejemplo n.º 4
0
        public void CollectorLogger_DoesNotCollectNonError()
        {
            // Arrange
            var innerLogger = new Mock <ILogger>();
            var collector   = new CollectorLogger(innerLogger.Object);

            // Act
            collector.LogDebug("Debug");
            collector.LogVerbose("Verbose");
            collector.LogInformation("Information");
            collector.LogWarning("Warning");
            var errors = collector.Errors.ToArray();

            // Assert
            Assert.Empty(errors);
        }
Ejemplo n.º 5
0
        public void CollectorLogger_PassesToInnerLogger()
        {
            // Arrange
            var innerLogger = new Mock <ILogger>();
            var collector   = new CollectorLogger(innerLogger.Object);

            // Act
            collector.LogDebug("Debug");
            collector.LogVerbose("Verbose");
            collector.LogInformation("Information");
            collector.LogWarning("Warning");
            collector.LogError("Error");

            // Assert
            innerLogger.Verify(x => x.LogDebug("Debug"), Times.Once);
            innerLogger.Verify(x => x.LogVerbose("Verbose"), Times.Once);
            innerLogger.Verify(x => x.LogInformation("Information"), Times.Once);
            innerLogger.Verify(x => x.LogWarning("Warning"), Times.Once);
            innerLogger.Verify(x => x.LogError("Error"), Times.Once);
        }
Ejemplo n.º 6
0
        public void CollectorLogger_CollectsErrors()
        {
            // Arrange
            var innerLogger = new Mock <ILogger>();
            var collector   = new CollectorLogger(innerLogger.Object);

            // Act
            var errorsStart = collector.Errors.ToArray();

            collector.LogError("ErrorA");
            var errorsA = collector.Errors.ToArray();

            collector.LogError("ErrorB");
            collector.LogError("ErrorC");
            var errorsAbc = collector.Errors.ToArray();
            var errordEnd = collector.Errors.ToArray();

            // Assert
            Assert.Empty(errorsStart);
            Assert.Equal(new[] { "ErrorA" }, errorsA);
            Assert.Equal(new[] { "ErrorA", "ErrorB", "ErrorC" }, errorsAbc);
            Assert.Equal(new[] { "ErrorA", "ErrorB", "ErrorC" }, errordEnd);
        }
Ejemplo n.º 7
0
        public void CollectData()
        {
            logger = new CollectorLogger(SourceServerInstance, CollectionSetUid, ItemId);


            if (verbose)
            {
                logger.logMessage("--------------------------------");
            }
            if (verbose)
            {
                logger.logMessage("    ExtendedXEReaderCollector   ");
            }
            if (verbose)
            {
                logger.logMessage("--------------------------------");
            }
            if (verbose)
            {
                logger.logMessage("Copyright© sqlconsulting.it 2014");
            }
            if (verbose)
            {
                logger.logMessage("-");
            }

            if (verbose)
            {
                logger.logMessage("Loading configuration");
            }
            //
            // Load Configuration
            //
            cfg = new XEReaderCollectorConfig();
            cfg.readFromDatabase(SourceServerInstance, CollectionSetUid, ItemId);

            String connectionString = String.Format(@"Data Source = {0}; Initial Catalog = master; Integrated Security = SSPI", SourceServerInstance);

            collectorThread = Thread.CurrentThread;
            Task.Factory.StartNew(() => checkCollectionSetEnabled());

            if (verbose)
            {
                logger.logMessage("Entering collection items loop");
            }
            foreach (CollectionItemConfig item in cfg.collectionItems)
            {
                XEReaderCollectionItemConfig itm = (XEReaderCollectionItemConfig)item;
                if (verbose)
                {
                    logger.logMessage("Processing item n. " + itm.Index);
                }
                if (verbose)
                {
                    logger.logMessage("Processing session " + itm.SessionDefinition);
                }


                var dataQueue = new ConcurrentQueue <DataTable>();



                DateTime lastEventFlush = new DateTime(1900, 1, 1);

                CheckSession(itm);

                Task.Factory.StartNew(() => PerformWrite(dataQueue, itm));

                // Queries an existing session
                Microsoft.SqlServer.XEvent.Linq.QueryableXEventData events = new QueryableXEventData(
                    connectionString,
                    itm.SessionName,
                    EventStreamSourceOptions.EventStream,
                    EventStreamCacheOptions.DoNotCache);

                foreach (PublishedEvent evt in events)
                {
                    try
                    {
                        DataTable dt = ReadEvent(evt);

                        //
                        // Apply filter
                        //
                        DataView dw = new DataView(dt);
                        dw.RowFilter = itm.Filter;
                        dt           = dw.ToTable();

                        //
                        // Enqueue the collected data for the consumer thread
                        //
                        if (dt != null && dt.Rows.Count > 0)
                        {
                            dataQueue.Enqueue(dt);
                        }


                        //
                        // Process rows to fire alerts if needed
                        //
                        foreach (AlertConfig currentAlert in itm.Alerts)
                        {
                            foreach (DataRow currentRow in dt.Select(currentAlert.Filter))
                            {
                                //TODO: Process alerts
                                ProcessAlert(currentAlert, currentRow);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        // capture the session related exceptions
                        logger.logMessage(e.StackTrace);

                        // try restarting the session event stream
                        try
                        {
                            events = new QueryableXEventData(
                                connectionString,
                                itm.SessionName,
                                EventStreamSourceOptions.EventStream,
                                EventStreamCacheOptions.DoNotCache);
                        }
                        catch (Exception ex)
                        {
                            // Unable to restart the events stream
                            logger.logMessage(ex.StackTrace);
                            throw ex;
                        }
                    }
                }
            }


            logger.cleanupLogFiles(cfg.DaysUntilExpiration);
        }
Ejemplo n.º 8
0
        private async Task <RestoreSummary> PerformNuGetV2RestoreAsync(PackageRestoreInputs packageRestoreInputs)
        {
            ReadSettings(packageRestoreInputs);
            var packagesFolderPath = GetPackagesFolder(packageRestoreInputs);

            var sourceRepositoryProvider = new CommandLineSourceRepositoryProvider(SourceProvider);
            var nuGetPackageManager      = new NuGetPackageManager(sourceRepositoryProvider, Settings, packagesFolderPath);

            var installedPackageReferences = new HashSet <Packaging.PackageReference>(new PackageReferenceComparer());

            if (packageRestoreInputs.RestoringWithSolutionFile)
            {
                installedPackageReferences.AddRange(packageRestoreInputs
                                                    .PackagesConfigFiles
                                                    .SelectMany(file => GetInstalledPackageReferences(file, allowDuplicatePackageIds: true)));
            }
            else if (packageRestoreInputs.PackagesConfigFiles.Count > 0)
            {
                // By default the PackageReferenceFile does not throw
                // if the file does not exist at the specified path.
                // So we'll need to verify that the file exists.
                Debug.Assert(packageRestoreInputs.PackagesConfigFiles.Count == 1,
                             "Only one packages.config file is allowed to be specified " +
                             "at a time when not performing solution restore.");

                var packageReferenceFile = packageRestoreInputs.PackagesConfigFiles[0];
                if (!File.Exists(packageReferenceFile))
                {
                    var message = string.Format(
                        CultureInfo.CurrentCulture,
                        LocalizedResourceManager.GetString("RestoreCommandFileNotFound"),
                        packageReferenceFile);

                    throw new InvalidOperationException(message);
                }

                installedPackageReferences.AddRange(
                    GetInstalledPackageReferences(packageReferenceFile, allowDuplicatePackageIds: true));
            }

            // EffectivePackageSaveMode is None when -PackageSaveMode is not provided by the user. None is treated as
            // Defaultv3 for V3 restore and should be treated as Defaultv2 for V2 restore. This is the case in the
            // actual V2 restore flow and should match in this preliminary missing packages check.
            var packageSaveMode = EffectivePackageSaveMode == Packaging.PackageSaveMode.None ?
                                  Packaging.PackageSaveMode.Defaultv2 :
                                  EffectivePackageSaveMode;

            var missingPackageReferences = installedPackageReferences.Where(reference =>
                                                                            !nuGetPackageManager.PackageExistsInPackagesFolder(reference.PackageIdentity, packageSaveMode)).ToArray();

            if (missingPackageReferences.Length == 0)
            {
                var message = string.Format(
                    CultureInfo.CurrentCulture,
                    LocalizedResourceManager.GetString("InstallCommandNothingToInstall"),
                    "packages.config");

                Console.LogMinimal(message);
                return(new RestoreSummary(true));
            }

            var packageRestoreData = missingPackageReferences.Select(reference =>
                                                                     new PackageRestoreData(
                                                                         reference,
                                                                         new[] { packageRestoreInputs.RestoringWithSolutionFile
                                ? packageRestoreInputs.DirectoryOfSolutionFile
                                : packageRestoreInputs.PackagesConfigFiles[0] },
                                                                         isMissing: true));

            var packageSources = GetPackageSources(Settings);

            var repositories = packageSources
                               .Select(sourceRepositoryProvider.CreateRepository)
                               .ToArray();

            var installCount = 0;
            var failedEvents = new ConcurrentQueue <PackageRestoreFailedEventArgs>();

            var packageRestoreContext = new PackageRestoreContext(
                nuGetPackageManager,
                packageRestoreData,
                CancellationToken.None,
                packageRestoredEvent: (sender, args) => { Interlocked.Add(ref installCount, args.Restored ? 1 : 0); },
                packageRestoreFailedEvent: (sender, args) => { failedEvents.Enqueue(args); },
                sourceRepositories: repositories,
                maxNumberOfParallelTasks: DisableParallelProcessing
                        ? 1
                        : PackageManagementConstants.DefaultMaxDegreeOfParallelism);

            CheckRequireConsent();

            var collectorLogger = new CollectorLogger(Console);
            var projectContext  = new ConsoleProjectContext(collectorLogger)
            {
                PackageExtractionContext = new PackageExtractionContext(collectorLogger)
            };

            if (EffectivePackageSaveMode != Packaging.PackageSaveMode.None)
            {
                projectContext.PackageExtractionContext.PackageSaveMode = EffectivePackageSaveMode;
            }

            using (var cacheContext = new SourceCacheContext())
            {
                cacheContext.NoCache        = NoCache;
                cacheContext.DirectDownload = DirectDownload;

                var downloadContext = new PackageDownloadContext(cacheContext, packagesFolderPath, DirectDownload);

                var result = await PackageRestoreManager.RestoreMissingPackagesAsync(
                    packageRestoreContext,
                    projectContext,
                    downloadContext);

                if (downloadContext.DirectDownload)
                {
                    GetDownloadResultUtility.CleanUpDirectDownloads(downloadContext);
                }

                return(new RestoreSummary(
                           result.Restored,
                           "packages.config projects",
                           Settings.Priority.Select(x => Path.Combine(x.Root, x.FileName)),
                           packageSources.Select(x => x.Source),
                           installCount,
                           collectorLogger.Errors.Concat(failedEvents.Select(e => e.Exception.Message))));
            }
        }
Ejemplo n.º 9
0
        static void Main(string[] args)
        {
            var options = new Options();

            if (!CommandLine.Parser.Default.ParseArguments(args, options))
            {
                return;
            }

            bool   verbose = options.Verbose;
            String SourceServerInstance = options.ServerInstance;

            CollectorLogger logger = new CollectorLogger(SourceServerInstance);

            string mutex_id = "Global\\ExtendedXEReaderCollector_" + SourceServerInstance.Replace("\\", "_");

            using (Mutex mutex = new Mutex(false, mutex_id))
            {
                if (!mutex.WaitOne(0, false))
                {
                    if (verbose)
                    {
                        logger.logMessage("Shut down. Only one instance of this program is allowed.");
                    }
                    return;
                }

                try
                {
                    if (verbose)
                    {
                        logger.logMessage("Starting");
                    }

                    // Instantiate a task that loads Collection Items from the database
                    Task.Factory.StartNew(() => loadXECollectionItems(SourceServerInstance, verbose));

                    Thread.Sleep(10000);

                    while (runningTasks.Count > 0 || initializing)
                    {
                        foreach (CollectionItemTask currentTask in runningTasks.Values)
                        {
                            if (currentTask.IsCompleted)
                            {
                                CollectionItemTask v = null;
                                runningTasks.TryRemove(currentTask.GetKey(), out v);
                                if (verbose)
                                {
                                    logger.logMessage("Task " + v.CollectionSetUid + " is completed.");
                                }
                            }
                        }
                        Thread.Sleep(100);
                    }
                    if (verbose)
                    {
                        logger.logMessage("Running tasks " + runningTasks.Count + ". Shutting down.");
                    }

                    if (verbose)
                    {
                        logger.logMessage("Ending with success");
                    }

                    System.Environment.Exit(0);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                    if (verbose)
                    {
                        logger.logMessage("Ending with failure");
                    }
                    if (verbose)
                    {
                        logger.logMessage(e.Message);
                    }
                    if (verbose)
                    {
                        logger.logMessage(e.StackTrace.ToString());
                    }
                }
            }
        }
Ejemplo n.º 10
0
        public void CollectData(
            String SourceServerInstance,
            Guid CollectionSetUid,
            int ItemId
            )
        {
            CollectorLogger logger = new CollectorLogger(SourceServerInstance, CollectionSetUid, ItemId);

            DataTable collectedData = null;

            if (verbose)
            {
                logger.logMessage("--------------------------------");
            }
            if (verbose)
            {
                logger.logMessage("      ExtendedTSQLCollector      ");
            }
            if (verbose)
            {
                logger.logMessage("--------------------------------");
            }
            if (verbose)
            {
                logger.logMessage("Copyright© sqlconsulting.it 2014");
            }
            if (verbose)
            {
                logger.logMessage("-");
            }

            if (verbose)
            {
                logger.logMessage("Loading configuration");
            }
            //
            // Load Configuration
            //
            TSQLCollectorConfig cfg = new TSQLCollectorConfig();

            cfg.readFromDatabase(SourceServerInstance, CollectionSetUid, ItemId);


            if (verbose)
            {
                logger.logMessage("Entering collection items loop");
            }
            foreach (CollectionItemConfig item in cfg.collectionItems)
            {
                TSQLCollectionItemConfig itm = (TSQLCollectionItemConfig)item;
                if (verbose)
                {
                    logger.logMessage("Processing item n. " + itm.Index);
                }
                if (verbose)
                {
                    logger.logMessage("Processing query " + itm.Query);
                }

                collectedData = null;

                String ts          = DateTime.Now.ToString("yyyyMMddHHmmss");
                String collectorId = CollectorUtils.getCacheFilePrefix(SourceServerInstance, CollectionSetUid, ItemId) + "_" + itm.Index;

                String destFile = Path.Combine(cfg.CacheDirectory, collectorId + "_" + ts + ".cache");

                //
                // Iterate through the enabled databases
                //
                if (verbose)
                {
                    logger.logMessage("Entering databases loop");
                }
                foreach (String currentDatabase in cfg.Databases)
                {
                    if (verbose)
                    {
                        logger.logMessage("Processing database " + currentDatabase);
                    }
                    //
                    // Execute the query in the collection item
                    //

                    DataTable dt = CollectorUtils.GetDataTable(SourceServerInstance, currentDatabase, itm.Query);

                    //
                    // Add computed columns
                    //
                    if (dt.Columns.Contains("database_name"))
                    {
                        dt.Columns["database_name"].ColumnName = "__database_name";
                    }
                    DataColumn cl_db = new DataColumn("database_name", typeof(String));
                    cl_db.DefaultValue = currentDatabase;
                    dt.Columns.Add(cl_db);


                    if (dt.Columns.Contains("collection_time"))
                    {
                        dt.Columns["collection_time"].ColumnName = "__collection_time";
                    }
                    DataColumn cl_dt = new DataColumn("collection_time", typeof(DateTime));
                    cl_dt.DefaultValue = DateTime.Now;
                    dt.Columns.Add(cl_dt);


                    //
                    // Merge collected data in a single DataTable
                    //
                    if (collectedData != null)
                    {
                        collectedData.Merge(dt);
                    }
                    else
                    {
                        collectedData = dt;
                        collectedData.DataSet.RemotingFormat = System.Data.SerializationFormat.Binary;
                        collectedData.RemotingFormat         = System.Data.SerializationFormat.Binary;
                    }
                }

                if (verbose)
                {
                    logger.logMessage("Saving to cache file " + destFile);
                }
                //
                // Save data to a binary cache file
                //
                if (File.Exists(destFile))
                {
                    File.Delete(destFile);
                }



                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter fm = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

                using (FileStream fs = new FileStream(destFile, FileMode.CreateNew))
                {
                    fm.Serialize(fs, collectedData);
                    fs.Close();
                }
                if (verbose)
                {
                    logger.logMessage("File saved successfully");
                }
            }


            logger.cleanupLogFiles(cfg.DaysUntilExpiration);
        }
Ejemplo n.º 11
0
 public LoggingMiddleware(CollectorLogger managerLogger)
 {
     ManagerLogger = managerLogger;
 }
Ejemplo n.º 12
0
 public LogsProvider(CollectorLogger managerLogger)
 {
     ManagerLogger = managerLogger;
 }