static void WriteLog(String FullPath, String Log)
{
try
{
if (File.Exists(FullPath))
{
FileInfo Fi = new FileInfo(FullPath);
if (Fi.Length > MaxFileSize * 1024)
{
if (File.Exists(FullPath + ".Old"))
{
File.Delete(FullPath + ".Old");
}
File.Move(FullPath, FullPath + ".Old");
}
}
string currentFile = System.IO.Path.GetFileName(new System.Diagnostics.StackTrace(true).GetFrame(0).GetFileName());
int currentLine = new System.Diagnostics.StackTrace(true).GetFrame(0).GetFileLineNumber();
File.AppendAllText(@FullPath, "[ " + MyTime.Now.ToString("dd/MM/yy HH:mm:ss") + " ] " + currentFile + "," + currentLine + "," + Log.Replace("\r\n", "\r\n ") + "\r\n");
}
catch (Exception ex)
{
utilities.Log(ex.ToString());
}
}
嗨,我正在我的程序中编写一个日志记录实用程序。日志的一部分,我想在使用堆栈跟踪的程序执行中显示当前文件名和当前行号。但是,然后以下代码行将返回日志实用程序的当前文件和行。
string currentFile = System.IO.Path.GetFileName(new System.Diagnostics.StackTrace(true).GetFrame(0).GetFileName());
int currentLine = new System.Diagnostics.StackTrace(true).GetFrame(0).GetFileLineNumber();
如何让堆栈跟踪返回调用函数的当前文件和行号?