示例#1
0
        private bool TryCreateCollector(IConsole console, CollectionConfiguration config, out EventCollector collector)
        {
            collector = null;

            if (Etw)
            {
                if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    console.Error.WriteLine("Error: ETW-based collection is only supported on Windows.");
                    return(false);
                }

                if (!string.IsNullOrEmpty(ConfigPath))
                {
                    console.Error.WriteLine("WARNING: The '-c' option is ignored when using ETW-based collection.");
                }
                collector = new EtwCollector(config);
                return(true);
            }
            else
            {
                if (File.Exists(ConfigPath))
                {
                    console.Error.WriteLine("Config file already exists, tracing is already underway by a different consumer.");
                    return(false);
                }

                collector = new EventPipeCollector(config, ConfigPath);
                return(true);
            }
        }
示例#2
0
        public async Task <int> OnExecuteAsync(IConsole console, CommandLineApplication app)
        {
            if (ListProfiles)
            {
                WriteProfileList(console.Out);
                return(0);
            }
            if (!string.IsNullOrEmpty(KeywordsForProvider))
            {
                return(ExecuteKeywordsForAsync(console));
            }

            var config = new CollectionConfiguration()
            {
                ProcessId  = ProcessId,
                CircularMB = CircularMB,
                OutputPath = string.IsNullOrEmpty(OutputDir) ? Directory.GetCurrentDirectory() : OutputDir
            };

            if (Profiles != null && Profiles.Count > 0)
            {
                foreach (var profile in Profiles)
                {
                    if (!KnownData.TryGetProfile(profile, out var collectionProfile))
                    {
                        console.Error.WriteLine($"Unknown profile name: '{profile}'. See 'dotnet-collect --list-profiles' to get a list of profiles.");
                        return(1);
                    }
                    config.AddProfile(collectionProfile);
                }
            }

            if (Providers != null && Providers.Count > 0)
            {
                foreach (var provider in Providers)
                {
                    if (!EventSpec.TryParse(provider, out var spec))
                    {
                        console.Error.WriteLine($"Invalid provider specification: '{provider}'. See 'dotnet-collect --help' for more information.");
                        return(1);
                    }
                    config.Providers.Add(spec);
                }
            }

            if (!NoDefault)
            {
                // Enable the default profile if nothing is specified
                if (!KnownData.TryGetProfile(CollectionProfile.DefaultProfileName, out var defaultProfile))
                {
                    console.Error.WriteLine("No providers or profiles were specified and there is no default profile available.");
                    return(1);
                }
                config.AddProfile(defaultProfile);
            }

            if (!TryCreateCollector(console, config, out var collector))
            {
                return(1);
            }

            // Write the config file contents
            await collector.StartCollectingAsync();

            console.WriteLine("Tracing has started. Press Ctrl-C to stop.");

            await console.WaitForCtrlCAsync();

            await collector.StopCollectingAsync();

            console.WriteLine($"Tracing stopped. Trace files written to {config.OutputPath}");

            return(0);
        }
示例#3
0
 public EtwCollector(CollectionConfiguration config)
 {
     _config = config;
 }
示例#4
0
 public EventPipeCollector(CollectionConfiguration config, string configPath)
 {
     _config     = config;
     _configPath = configPath;
 }