c# - Unwrap exception stack -
I have a common method in my code that logs errors, something like
< Code> static zero logarre (exception preceded string noun name) {// real logging is more complex console. WrightLine (methodName); Console.WriteLine (ex); }Now, in every method, I have to use try / hold in some form
static zero testMethod1 () {try {// do Actual operation here string S = faucet; s = s.Substring (1); } Hold (exception exec) {LogError (exec, System.Reflection.MethodBase.GetCurrentMethod (.name)); }}
If there is an exception, the logged information will be displayed the exact method name
TestMethod1 .
Although I do not like this work to add effort / grip in every method, so I thought of writing such a replacement
Static Zero DoWithLog (Action A ) {Try {a (); } Hold (Exception Pre) {LogError (formerly, System.Reflection.MethodBase.GetCurrentMethod (.name)); }} Private Static Zero TestMethod2 () {DoWithLog ((=) ({string s = null; s = s.Substring (1);}); }
In this method, I'm actually writing the necessary code with trying / catching. Although it is working as expected, I can not find the name of the original method, I get the name of Lambda. There is no way to get an exception stack, the original method name
TestMethod2 ?
PS I know that it is not good to engage in everything in trying / catching and a global exception handler should be used, but how this project was written to me very much, and Nobody wants to change it.
This should work.
StackTrace stackTrace = new stacktrace (); Methodbase Method = stackTrace.GetFrame (1) .GetMethod (); Console.WriteLine (methodBase.Name);
taken from the same question:
Comments
Post a Comment