2017-11-19 4 views
0

컴퓨터에 연결된 Android 장치에서 실행중인 앱의 logcat 파일을 수집하는 작은 C# 응용 프로그램을 작성 중입니다.logcat에서 로깅을 중지하고 adb를 프로그래밍 방식으로 종료하는 방법은 무엇입니까?

나는 쉽게 logcat을 시작하고 특정 텍스트 파일에 원하는 줄을 기록 할 수 있습니다. 하지만 로깅에서 logcat을 중지하려고 시도한 모든 명령이 작동하지 않습니다.

관리자 권한으로 내 앱을 실행할 때도 솔루션을 사용해 보았습니다.

static void Main(string[] args) 
    { 
     string choice; 
     string clearLogging = @"adb logcat -c"; 
     string startLogging = @"adb logcat MyApp_LoggingTag:V AndroidRuntime:E *:S > C:\logcat.txt"; 
     string adbDir = @"C:\Users\me\AppData\Local\Android\android-sdk\platform-tools\"; 


     string clearCommand = adbDir + clearLogging; 
     string startLoggingCommand = adbDir + startLogging; 

     ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe", "/K " + clearCommand); 
     startInfo.CreateNoWindow = true; 
     startInfo.UseShellExecute = false; 
     startInfo.RedirectStandardInput = true; 

     //Tried giving the cmd process elevated rights and then use logcat -c - didn't work 
     //startInfo.Verb = "runas"; 

     Process logcatRunner = Process.Start(startInfo); 

     //This works! 
     logcatRunner.StandardInput.WriteLine(startLoggingCommand); 

     Console.WriteLine("Logging has started."); 
     Console.Write("Press Enter to stop logging...."); 
     Console.ReadLine(); 

     //-c doesn't work 
     //logcatRunner.StandardInput.WriteLine(clearCommand); 
     //Tried killing adb via the logcatRunner process - doesn't work. 
     //logcatRunner.StandardInput.WriteLine(@"taskkill -f /im ""adb.exe"""); 
     //Tried killing my process - doesn't work - adb is still running and logcat is still writing logs 
     //logcatRunner.Kill(); 

     Console.WriteLine("Logging has stopped."); 
     Console.Write(@"Enter any key"); 
     choice = Console.ReadLine(); 

    } 

ADB는 아직도 내가 위의 응용 프로그램을 종료 한 후 실행 :

여기 내 코드입니다.
제 질문은 adb를 시작하고 logcat을 성공적으로 시작한 것입니다. 프로그래밍 방식으로 두 프로그램을 모두 닫는 방법은 무엇입니까?

+0

나머지'adb.exe' 과정이 정말 귀찮게하는 경우 - 단지 –

답변

1

이 방법을 사용하는 것은 복잡합니다. cmd 프로세스를 만든 다음 다른 프로세스 (adb)를 시작합니다. adb를 죽이려면 CTRL + C를 cmd에 보내야하지만, CreateNoWindow=true 때문에 쉽지 않습니다. 나는 그것의 출력을 리디렉션, 직접 다른 접근 방법과 실행 ADB을 건의 할 것입니다 :

string adbPath = @"G:\Android\android-sdk\platform-tools\adb.exe"; 
ProcessStartInfo startInfo = new ProcessStartInfo(adbPath, "logcat MyApp_LoggingTag:V AndroidRuntime:E *:S"); 
startInfo.CreateNoWindow = true; 
startInfo.UseShellExecute = false; 
startInfo.RedirectStandardOutput = true;   
// if you don't want to recreate it each time - choose another file mode, like FileMode.Append 
using (var fs = new FileStream(@"C:\logcat.txt", FileMode.Create, FileAccess.Write, FileShare.Read)) { 
    using (var writer = new StreamWriter(fs)) {      
     Process logcatRunner = new Process(); 
     logcatRunner.StartInfo = startInfo; 
     logcatRunner.EnableRaisingEvents = true; 
     logcatRunner.OutputDataReceived += (sender, args) => { 
      // Data null indicates end of output stream - don't write it 
      if (args.Data != null) { 
       writer.Write(args.Data); 
       // flush immediately if needed 
       writer.Flush(); 
      } 
     }; 
     logcatRunner.Start(); 
     logcatRunner.BeginOutputReadLine(); 
     Console.WriteLine("Logging started, press any key to stop"); 
     Console.ReadKey(); 
     logcatRunner.CancelOutputRead(); 
     logcatRunner.Kill(); 
     logcatRunner.WaitForExit(); 
    } 
} 
+0

감사합니다 결국'ADB 죽일-server'을 실행! 원래 cmd에서 배치 파일을 실행하고 있었지만 요구 사항이 너무 간단합니다. 파일 스트림도 훨씬 좋은 아이디어입니다. 나는 기회를 얻 자마자 시험해 볼 것입니다 ... –

+0

괜찮습니다. 고마워요. –

관련 문제