当前位置: 首页 > 编程语言 > C#

如何使用C#获取报错信息分享

时间:2023-04-10 20:58:38 C#

C#如何获取报错信息对于vsinstr-coveragehello.exe,我可以使用如下所示的C#代码。Processp=newProcess();StringBuildersb=newStringBuilder("/COVERAGE");sb.Append("hello.exe");p.StartInfo.FileName="vsinstr.exe";p.StartInfo.Arguments=sb.ToString();p.开始();p.WaitForExit();发生错误时,我收到错误消息:错误VSP1018:VSInstr不支持处理已检测的二进制文件。.如何使用C#获取此错误消息?解决了我可以从答案中得到错误信息。使用系统;使用系统文本;使用系统诊断;//您必须添加对Microsoft.VisualStudio.Coverage.Monitor.dll的引用namespaceLvFpga{classCov2xml{staticvoidMain(string[]args){Processp=newProcess();p.StartInfo.RedirectStandardOutput=true;p.StartInfo.RedirectStandardError=true;p.StartInfo.UseShellExecute=false;StringBuildersb=newStringBuilder("/COVERAGE");sb.Append("helloclass.exe");p.StartInfo.FileName="vsinstr.exe";p.StartInfo.Arguments=sb.ToString();p.开始();字符串stdoutx=p.StandardOutput.ReadToEnd();字符串stderrx=p.StandardError.ReadToEnd();p.WaitForExit();Console.WriteLine("退出代码:{0}",p.ExitCode);Console.WriteLine("标准输出:{0}",stdoutx);Console.WriteLine("标准错误:{0}",stderrx);}}}您需要重新确定向标准输出或标准错误。以下是标准输出的代码示例:Processp=newProcess();p.StartInfo.FileName="你好.exe";p.StartInfo.RedirectStandardOutput=true;p.StartInfo.UseShellExecute=false;p.开始();字符串标准输出=p.StandardOutput.ReadToEnd();p.WaitForExit();您必须重定向StdError并读取流以捕获错误。System.Diagnostics.ProcessStartInfoprocessStartInfo=newSystem.Diagnostics.ProcessStartInfo("MyExe.exe","parameters...");int退出代码=0;processStartInfo.RedirectStandardError=true;processStartInfo.RedirectStandardOutput=true;processStartInfo=true;;processStartInfo.UseShellExecute=false;System.Diagnostics.Process进程=System.Diagnostics.Process.Start(processStartInfo);过程.WaitForExit();//等待20秒exitCode=process.ExitCode;字符串标准输出=process.StandardOutput。读到结束();字符串stderr=process.StandardError.ReadToEnd();//你应该在stdout或stderr中看到错误字符串,这取决于你的控制台应用程序如何决定输出失败。您必须检索标准/错误输出。方法如下:标准输出错误输出假设vsinstr.exe进程将错误写入标准错误流,您可以通过捕获进程的标准错误流来获取错误消息。为此,您需要将ProcessStartInfo类的RedirectStandardError属性设置为true,向Process类的ErrorDataReceived事件添加事件处理程序,并在启动vsinstr.exe进程之前调用Process类的BeginErrorReadLine方法。这是一些示例代码:usingSystem;使用System.Collections.Generic;使用系统诊断;使用系统文本;使用系统线程;类CaptureProcessOutput{privateManualResetEventm_processExited=newManualResetEvent(false);私有列表m_errorMessages=newList();私有列表m_regularMessages=newList();私有进程m_process=newProcess();publicCaptureProcessOutput(){}publicvoidRun(string[]args){StringBuildersb=newStringBuilder("/COVERAGE");sb.Append("hello.exe");m_process.StartInfo.FileName="vsinstr.exe";m_process.StartInfo.Arguments=sb.ToString();m_process.StartInfo.UseShellExecute=false;m_process.Exited+=this.ProcessExited;m_process.StartInfo.RedirectStandardError=true;m_process.StartInfo.RedirectStandardOutput=true;m_process.ErrorDataReceived+=this.ErrorDataHandler;m_process.OutputDataReceived+=this.OutputDataHandler;m_process.BeginErrorReadLine();m_process.BeginOutputReadLine();m_process.Start();m_processExited.WaitOne();}privatevoidErrorDataHandler(objectsender,DataReceivedEventArgsargs){stringmessage=args.Data;if(message.StartsWith("Error")){//vsinstr.exe进程报错m_errorMessages.Add(message);}}privatevoidOutputDataHandler(objectsender,DataReceivedEventArgsargs){stringmessage=args.Data;m_regularMessages.Add(消息);}privatevoidProcessExited(objectsender,EventArgsargs){//这是你可以添加一些代码的地方//在这个程序退出之前执行。m_processExited.Set();}publicstaticvoidMain(string[]args){CaptureProcessOutputcpo=newCaptureProcessOutput();cpo.Run(args);}}呀——在try/catch块里面?我错了怎么了?try{Processp=newProcess();StringBuildersb=newStringBuilder("/COVERAGE");sb.Append("hello.exe");p.StartInfo.FileName="vsinstr.exe";p.StartInfo.Arguments=sb.ToString();p.开始();p.WaitForExit();}catch(异常前){//Handleexception}...请记住,ex.Message可能不包含您在上面指定的异常,而内部异常(例如ex.InnerException)可能我假设您在调试时看到了此消息。如果这不是您想要的,请告诉我,但您可以使用简单的trycatch块。以上就是C#学习教程:如何使用C#获取错误信息分享的全部内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注——try{//CODEGOESHERE}catch(Exceptionex){System.Diagnostics.Debug.WriteLine(ex.信息);}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: