Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get the calling method properties in c#
    text
    copied!<p>I am currently writing an Error logger for my application, and basically what I am interested in, is when handling the exception, I invoke a function <em>CreateLogEntry(...)</em> from my logging class. The idea is, I want <em>CreateLogEntry(...)</em> to know it was called from within a method <em>BadFunction(...)</em> and retrieve its parameters as part of the logging process.</p> <p>Now I already did some homework (I guess not enough though :S), and I am sure my solution lies in the <em>System.Reflection</em>, and already found some examples on how to enumerate all functions within a class (<a href="http://www.csharp-examples.net/get-method-names/" rel="nofollow">http://www.csharp-examples.net/get-method-names/</a>). But would it be possible to get the name of the function that creating that exception.</p> <p>Ex (what I got):</p> <pre><code> public string GetCurrentMode() { string currentMode = ""; try { currentMode = _host.Mode.ToString(); } catch(Exception ex) { Common.MorpheeException me = new Common.MorpheeException(); me.MethodName = "GetCurrentMode"; me.NameSpace = "MorpheeScript"; me.ErrorNumber = 0; me.ErrorDescription = ex.Message; me.StackTrace = ex.StackTrace; throw new FaultException&lt;Common.MorpheeException&gt;(me); } return currentMode; } </code></pre> <p>What I want to have:</p> <pre><code> public string GetCurrentMode() { string currentMode = ""; try { currentMode = _host.Mode.ToString(); } catch(Exception ex) { Common.MorpheeException me = new Common.MorpheeException(ex); // Here me should know that it is called from GetCurrentMode() and // retrieve the parameters if there are any throw new FaultException&lt;Common.MorpheeException&gt;(me); } return currentMode; } </code></pre> <p>Many thanks for your help</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload