public void SetJobTraceListener(JobTraceListener jobTraceListener) { JobTraceListener = jobTraceListener; Trace.Listeners.Add(jobTraceListener); if (_jobTraceEventListener != null) { _jobTraceEventListener.Dispose(); } if (_jobEventSource != null) { _jobTraceEventListener = new JobTraceEventListener(JobTraceListener); _jobTraceEventListener.EnableEvents(_jobEventSource, EventLevel.LogAlways); } }
public JobTraceEventListener(JobTraceListener jobTraceListener) { _jobTraceListener = jobTraceListener; }
private static IDictionary<string, string> ParseArgsDictionary(string[] commandLineArgs) { if (commandLineArgs.Length > 0 && string.Equals(commandLineArgs[0], "-dbg", StringComparison.OrdinalIgnoreCase)) { commandLineArgs = commandLineArgs.Skip(1).ToArray(); Debugger.Launch(); } // Get the args passed in or provided as an env variable based on jobName as a dictionary of <string argName, string argValue> var jobTraceListener = new JobTraceListener(); Trace.Listeners.Add(jobTraceListener); var jobArgsDictionary = JobConfigurationManager.GetJobArgsDictionary(jobTraceListener, commandLineArgs, "Stats.RefreshClientDimension"); return jobArgsDictionary; }
/// <summary> /// Parses the string[] of <c>args</c> passed into the job into a dictionary of string, string. /// Expects the string[] to be set of pairs of argumentName and argumentValue, where, argumentName start with a hyphen /// </summary> /// <param name="jobTraceListener"></param> /// <param name="commandLineArgs">Arguments passed to the job via commandline or environment variable settings</param> /// <param name="jobName">Jobname to be used to infer environment variable settings</param> /// <returns>Returns a dictionary of arguments</returns> public static IDictionary<string, string> GetJobArgsDictionary(JobTraceListener jobTraceListener, string[] commandLineArgs, string jobName) { var allArgsList = commandLineArgs.ToList(); if (allArgsList.Count == 0) { Trace.TraceInformation("No command-line arguments provided. Trying to pick up from environment variable for the job..."); } var argsEnvVariable = "NUGETJOBS_ARGS_" + jobName; var envArgString = Environment.GetEnvironmentVariable(argsEnvVariable); if (string.IsNullOrEmpty(envArgString)) { Trace.TraceWarning("No environment variable for the job arguments was provided"); } else { allArgsList.AddRange(envArgString.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)); } Trace.TraceInformation("Total number of arguments : " + allArgsList.Count); // Arguments are expected to be a set of pairs, where each pair is of the form '-<argName> <argValue>' // Or, in singles as a switch '-<switch>' var argsDictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); for (int i = 0; i < allArgsList.Count; i++) { if (!allArgsList[i].StartsWith("-")) { throw new ArgumentException("Argument Name does not start with a hyphen ('-')"); } var argName = allArgsList[i].Substring(1); if (string.IsNullOrEmpty(argName)) { throw new ArgumentException("Argument Name is null or empty"); } var nextString = allArgsList.Count > i + 1 ? allArgsList[i + 1] : null; if (string.IsNullOrEmpty(nextString) || nextString.StartsWith("-")) { // If the key already exists, don't add. This means that first added value is preferred // Since command line args are added before args from environment variable, this is the desired behavior if (!argsDictionary.ContainsKey(argName)) { // nextString startWith hyphen, the current one is a switch argsDictionary.Add(argName, bool.TrueString); } } else { var argValue = nextString; if (string.IsNullOrEmpty(argValue)) { throw new ArgumentException("Argument Value is null or empty"); } // If the key already exists, don't add. This means that first added value is preferred // Since command line args are added before args from environment variable, this is the desired behavior if (!argsDictionary.ContainsKey(argName)) { argsDictionary.Add(argName, argValue); } i++; // skip next string since it was added as an argument value } } return argsDictionary; }
/// <summary> /// Parses the string[] of <c>args</c> passed into the job into a dictionary of string, string. /// Expects the string[] to be set of pairs of argumentName and argumentValue, where, argumentName start with a hyphen /// </summary> /// <param name="jobTraceListener"></param> /// <param name="commandLineArgs">Arguments passed to the job via commandline or environment variable settings</param> /// <param name="jobName">Jobname to be used to infer environment variable settings</param> /// <returns>Returns a dictionary of arguments</returns> public static IDictionary <string, string> GetJobArgsDictionary(JobTraceListener jobTraceListener, string[] commandLineArgs, string jobName) { var allArgsList = commandLineArgs.ToList(); if (allArgsList.Count == 0) { Trace.TraceInformation("No command-line arguments provided. Trying to pick up from environment variable for the job..."); } var argsEnvVariable = "NUGETJOBS_ARGS_" + jobName; var envArgString = Environment.GetEnvironmentVariable(argsEnvVariable); if (string.IsNullOrEmpty(envArgString)) { Trace.TraceWarning("No environment variable for the job arguments was provided"); } else { allArgsList.AddRange(envArgString.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)); } Trace.TraceInformation("Total number of arguments : " + allArgsList.Count); // Arguments are expected to be a set of pairs, where each pair is of the form '-<argName> <argValue>' // Or, in singles as a switch '-<switch>' var argsDictionary = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); for (int i = 0; i < allArgsList.Count; i++) { if (!allArgsList[i].StartsWith("-")) { throw new ArgumentException("Argument Name does not start with a hyphen ('-')"); } var argName = allArgsList[i].Substring(1); if (string.IsNullOrEmpty(argName)) { throw new ArgumentException("Argument Name is null or empty"); } var nextString = allArgsList.Count > i + 1 ? allArgsList[i + 1] : null; if (string.IsNullOrEmpty(nextString) || nextString.StartsWith("-")) { // If the key already exists, don't add. This means that first added value is preferred // Since command line args are added before args from environment variable, this is the desired behavior if (!argsDictionary.ContainsKey(argName)) { // nextString startWith hyphen, the current one is a switch argsDictionary.Add(argName, bool.TrueString); } } else { var argValue = nextString; if (string.IsNullOrEmpty(argValue)) { throw new ArgumentException("Argument Value is null or empty"); } // If the key already exists, don't add. This means that first added value is preferred // Since command line args are added before args from environment variable, this is the desired behavior if (!argsDictionary.ContainsKey(argName)) { argsDictionary.Add(argName, argValue); } i++; // skip next string since it was added as an argument value } } return(argsDictionary); }