[
stathread]
static void M
ain(string[]
args)
{
ReportError("Yay!");
}
static
PRivate void ReportError(string Message)
{
StackFr
ame CallStack = new StackFrame(1, true);
Console.Wr
ITe("Error: " + Message + ", File: " + CallStack.GetFileName() + ", Line: " + CallStack.GetFileLineNumber());
}
try
{
throw new Exception();
}
catch (Exception ex)
{
// Get stack trace for the exception with source file information
VAR st = new StackTrace(ex, true);
// Get the top stack frame
var frame = st.GetFrame(0);
// Get the line number
From the stack frame
var line = frame.GetFileLineNumber();
}
static void SomeMethodSomewhere()
{
ShowMessage("Boo");
}
...
static void ShowMessage(string message,
[CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null)
{
MessageBox.Show(message + " at line " + lineNumber + " (" + caller + ")");
}