【问题标题】:How to stop logcat from logging and close adb programmatically?如何停止 logcat 记录并以编程方式关闭 adb?
【发布时间】:2017-11-19 15:51:32
【问题描述】:

我正在编写一个小型 C# 应用程序来收集在连接到计算机的 Android 设备上运行的应用程序的 logcat 文件。

我可以轻松启动 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 后,如何以编程方式关闭它们?

【问题讨论】:

  • 如果剩余的adb.exe 进程让您如此困扰 - 最后运行adb kill-server

标签: c# process adb logcat


【解决方案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();
    }
}

【讨论】:

  • 谢谢!最初我是从 cmd 运行一个批处理文件,但它对我的要求来说太简单了。文件流也是一个更好的主意。有机会我会尽快测试的...
  • Process 也应该被处理或包裹在 using 块中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-20
  • 2015-08-31
  • 1970-01-01
  • 2023-03-28
相关资源
最近更新 更多