I am wanting to log out some INFO logging in a method, and I am not wanting to use reflection to get hold of the Class and Method name.
For Error logging I can get the stack off the exception but how do I do this without an exception to get the StackTrace off?
You can get the information about the caller using caller attributes
public void Log(Exception ex, [CallerFilePath]string callerFilePath = null, [CallerMemberName]string callerMemberName = null, [CallerLineNumber]int callerLineNumber = 0)
{
Console.WriteLine($"Message: {ex.Message} # File: {callerFilePath} # Line: {callerLineNumber} # Member: {callerMemberName}" );
}
When the method is called the information about the caller will be passes to you by the compiler, you don't need to manually specify the parameters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With