打开默认的文本编辑器,并在应用出口上关闭
使用.NET Core 6,当应用程序中发生未经治疗的异常时,尤其是负载错误时,我想将其转储到文本文件中并打开默认文本编辑器。
问题在于,该应用程序退出后立即被杀死!
process.waitforexit()不起作用,因为我没有直接启动该应用程序,而是启动了文本文件。
到目前为止,我能做的最好的方法是等待10秒钟,然后才退出该应用程序...我该怎么做得更好?解决方案需要跨平台工作。
if (logPath != null)
{
// Dump error to log file and open default text editor.
var log = logPath();
System.IO.File.WriteAllText(log, ex.ToString());
var notepad = new Process
{
StartInfo = new ProcessStartInfo(log)
{
UseShellExecute = true
}
};
notepad.Start();
Thread.Sleep(TimeSpan.FromSeconds(10));
}
编辑:我正在Linux中使用Jetbrains Rider。如果我直接在IDE外运行应用程序,则它将保持打开状态!
With .NET Core 6, When an unhandled exception occurs in the application, particularly a load error, I want to dump it into a text file and open the default text editor.
The problem is that the text editor gets killed as soon as the app exits!
Process.WaitForExit() doesn't work because I didn't start the app directly, but rather launched the text file.
So far, the best I could do is to wait 10 seconds before exiting the app... how can I do better? Solution needs to work cross-platform.
if (logPath != null)
{
// Dump error to log file and open default text editor.
var log = logPath();
System.IO.File.WriteAllText(log, ex.ToString());
var notepad = new Process
{
StartInfo = new ProcessStartInfo(log)
{
UseShellExecute = true
}
};
notepad.Start();
Thread.Sleep(TimeSpan.FromSeconds(10));
}
EDIT: I'm using Jetbrains Rider in Linux. If I run the application directly outside the IDE, then it stays open!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看代码会发生什么的最佳方法是使用Windows事件日志!
您可以简单地使用它:
有关更多帮助,请参阅此标题:
写入Windows Application Everspord lotect offect Epplication dob lide source source Registration
但是,如果您坚持在默认编辑器(如记事本)中坚持打开文本,则可以使用以下代码:
The best way to see what happens to your code is using Windows Event Log!
you can simply use it:
for more help see this title:
Write to Windows Application Event Log without event source registration
But if you insist on open text in a default editor like notepad, you can use the below code: