Considering that the debug data file is available (PDB) and by using either System.Reflection or another similar framework such as Mono.Cecil
Up to date method:
private static void Log(string text,
[CallerFilePath] string file = "",
[CallerMemberName] string member = "",
[CallerLineNumber] int line = 0)
{
Console.WriteLine("{0}_{1}({2}): {3}", Path.GetFileName(file), member, line, text);
}
New Framework API
which populates arguments (marked with special attributes) at runtime,
see more in my answer to this SO question
Using one of the methods explained above, inside the constructor of an attribute, you can provide the source location of everything, that may have an attribute - for instance a class. See the following attribute class:
sealed class ProvideSourceLocation : Attribute
{
public readonly string File;
public readonly string Member;
public readonly int Line;
public ProvideSourceLocation
(
[CallerFilePath] string file = "",
[CallerMemberName] string member = "",
[CallerLineNumber] int line = 0)
{
File = file;
Member = member;
Line = line;
}
public override string ToString() { return File + "(" + Line + "):" + Member; }
}
[ProvideSourceLocation]
class Test
{
...
}
The you can write for instance:
Console.WriteLine(typeof(Test).GetCustomAttribute<ProvideSourceLocation>(true));
Output will be:
a:\develop\HWClassLibrary.cs\src\Tester\Program.cs(65):
you might find some help with these links:
Getting file and line numbers without deploying the PDB files also found this following post
"Hi Mark,
The following will give you the line number of your code (in the source file):
Dim CurrentStack As System.Diagnostics.StackTrace
MsgBox (CurrentStack.GetFrame(0).GetFileLineNumber)
In case you're interested, you can find out about the routine that you're in, as well as all its callers.
Public Function MeAndMyCaller As String
Dim CurrentStack As New System.Diagnostics.StackTrace
Dim Myself As String = CurrentStack.GetFrame(0).GetMethod.Name
Dim MyCaller As String = CurrentStack.GetFrame(1).GetMethod.Name
Return "In " & Myself & vbCrLf & "Called by " & MyCaller
End Function
This can be very handy if you want a generalised error routine because it can get the name of the caller (which would be where the error occurred).
Regards, Fergus MVP [Windows Start button, Shutdown dialogue] "