class SomeClass
{
public int DoSomething()
{
ReportError("Here's an error message");
return 0;
}
private void ReportError(string Message)
{
// Get the frame one step up the call tree
StackFrame CallStack = new
StackFrame(1, true);
// These will now show the file and line number of the ReportError
call in the DoSomething() method
string SourceFile = CallStack.GetFileName(),
int SourceLine = CallStack.GetFileLineNumber(),
MyWriteToFile("Error: " + Message + " - File: " + SourceFile + "
Line: " + SourceLine.ToString());
}
}