static void Main(string[] args) { XmlConfigurator.ConfigureAndWatch(new FileInfo("log4net.config")); PrintHello(); CommandLineConfiguration config = CreateCommandLineConfiguration(); if (args == null || args.Length <= 0) { PrintUsage(config); return; } Dictionary <string, CommandLineSwitch> switches = new CommandLineParser().Parse(args, config); RunnerParameters parameters = AnalyzeCommandLineSwitches(switches, args); if (parameters == null) { return; } Process process = AttachToTargetProcess(parameters); if (process == null) { return; } if (!IsCompatibleProcess(process)) { return; } CopyLogifyAssembliesToTargetProcessDirectory(process.MainModule.FileName); InjectLogifyAssembly(process, process.Id, parameters.IsWinforms); //for (;;) { // Console.WriteLine("Press 'Q' to exit"); // ConsoleKeyInfo key = Console.ReadKey(); // if (key.Key == ConsoleKey.Q) // break; //} }
static Process AttachToTargetProcess(RunnerParameters parameters) { try { if (!String.IsNullOrEmpty(parameters.TargetProcessCommandLine)) { return(StartTargetProcess(parameters.TargetProcessCommandLine, parameters.TargetProcessArgs)); } else if (parameters.Pid != 0) { Process.EnterDebugMode(); return(Process.GetProcessById(parameters.Pid)); } else { return(null); } } catch { return(null); } }
static RunnerParameters AnalyzeCommandLineSwitches(Dictionary <string, CommandLineSwitch> switches, string[] args) { RunnerParameters result = new RunnerParameters(); if (!switches.ContainsKey("exec") && !switches.ContainsKey("pid")) { Logger.ErrorFormat("Please specify target process ID or command line"); return(null); } if (switches.ContainsKey("exec")) { CommandLineSwitch @switch = switches["exec"]; result.TargetProcessCommandLine = @switch.Values[0]; result.TargetProcessArgs = CreateTargetProcessArguments(args, @switch.Index + 1); } if (switches.ContainsKey("pid")) { CommandLineSwitch @switch = switches["pid"]; string pidText = @switch.Value; int pid = TryParsePid(pidText); if (pid == 0) { Logger.ErrorFormat("Unable to parse target process Id: {0}", pidText); return(null); } result.Pid = pid; } result.IsWinforms = switches.ContainsKey("win"); result.IsWpf = switches.ContainsKey("wpf"); if (result.IsWinforms == result.IsWpf) { Logger.ErrorFormat("Please specify target process type: --win or --wpf"); return(null); } return(result); }