[C#]如何在运行时出错的情况下写日志?

[C#]How to write log in runtime error situation?

如何在Runtime Error情况下写日志?

我想知道源代码哪里出错了(执行.exe文件时)

(我不使用Serial debugging

您可以使用 Debug.WriteLine

打印异常
catch (Exception ex)
{
    Debug.Writeline(ex.ToString());
}

使用 Compact 框架有一个问题,它不会有相同的行为。但是你可以试试这个。

On Windows CE, any information passed to Debug.Write is displayed in a temporary console window on the device. The console window automatically opens on the fist call to Debug.Write and closes immediately after the application exits. The console window can be kept open by placing a breakpoint at the end of the application's Main() function, to prevent the application from completely exiting.

更多详情 - https://msdn.microsoft.com/en-us/library/aa446495.aspx