示例#1
0
        public static bool TryParse(string input, out LoggerSpec spec)
        {
            var splat = input.Split(':');

            var    prefix = splat[0];
            string level  = null;

            if (splat.Length > 1)
            {
                if (!_levelMap.TryGetValue(splat[1], out level))
                {
                    spec = null;
                    return(false);
                }
            }

            spec = new LoggerSpec(prefix, level);
            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));
            }

            if (string.IsNullOrEmpty(ConfigPath))
            {
                ConfigPath = ConfigPathDetector.TryDetectConfigPath(ProcessId);
                if (string.IsNullOrEmpty(ConfigPath))
                {
                    console.Error.WriteLine("Couldn't determine the path for the eventpipeconfig file from the process ID. Specify the '--config-path' option to provide it manually.");
                    return(1);
                }
                console.WriteLine($"Detected config file path: {ConfigPath}");
            }

            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 providerSpec))
                    {
                        console.Error.WriteLine($"Invalid provider specification: '{provider}'. See 'dotnet-collect --help' for more information.");
                        return(1);
                    }
                    config.Providers.Add(providerSpec);
                }
            }

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

            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);
        }