3

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 CreateLogEntry(...) from my logging class. The idea is, I want CreateLogEntry(...) to know it was called from within a method BadFunction(...) and retrieve its parameters as part of the logging process.

Now I already did some homework (I guess not enough though :S), and I am sure my solution lies in the System.Reflection, and already found some examples on how to enumerate all functions within a class (http://www.csharp-examples.net/get-method-names/). But would it be possible to get the name of the function that creating that exception.

Ex (what I got):

    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<Common.MorpheeException>(me);
        }

        return currentMode;
    }

What I want to have:

        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<Common.MorpheeException>(me);
        }

        return currentMode;
    }

Many thanks for your help

Elio
  • 463
  • 6
  • 18

1 Answers1

1

In MorpheeException constructior you can find calling method using reflection and then use it.

Paweł Smejda
  • 1,879
  • 1
  • 17
  • 25