打开默认的文本编辑器,并在应用出口上关闭

发布于 2025-02-13 23:03:22 字数 721 浏览 1 评论 0原文

使用.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

So尛奶瓶 2025-02-20 23:03:22

查看代码会发生什么的最佳方法是使用Windows事件日志!

您可以简单地使用它:

using (EventLog eventLog = new EventLog("Application")) 
{
    eventLog.Source = "Application"; 
    eventLog.WriteEntry("Log message example", EventLogEntryType.Information, 101, 1); 
}

有关更多帮助,请参阅此标题:
写入Windows Application Everspord lotect offect Epplication dob lide source source Registration

但是,如果您坚持在默认编辑器(如记事本)中坚持打开文本,则可以使用以下代码:

StreamWriter sw = new StreamWriter("tmp_log.txt");
sw.WriteLine("Some errore result to show");
sw.Close();
new Process
{
     StartInfo = new ProcessStartInfo("tmp_log.txt")
     {
        UseShellExecute = true
      }
}.Start();

The best way to see what happens to your code is using Windows Event Log!

you can simply use it:

using (EventLog eventLog = new EventLog("Application")) 
{
    eventLog.Source = "Application"; 
    eventLog.WriteEntry("Log message example", EventLogEntryType.Information, 101, 1); 
}

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:

StreamWriter sw = new StreamWriter("tmp_log.txt");
sw.WriteLine("Some errore result to show");
sw.Close();
new Process
{
     StartInfo = new ProcessStartInfo("tmp_log.txt")
     {
        UseShellExecute = true
      }
}.Start();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文