views:

211

answers:

3

The situation is this. I have an asp.net webservice application...say a page called api.asmx

In the code behind I have several methods, for example:

[WebMethod(Description="Method1")]
public int GetSomething(int num1, int num2){
  try{
    return SomeObject.DatabaseCall.DoSomething(num1, num2);
  }
  catch(Exception ex){
    object[] pars = new object[] { num1, num2 };
    LogError("GetSomething", pars, ex);
  }
}

[WebMethod(Description="Method2")]
public int GetSomething2(string w, string j, int f){
  try{
    return AnotherObject.DoSomething(w, j, f);
  }
  catch(Exception ex){
    object[] pars = new object[] { w, j, f };
    LogError("GetSomething2", pars, ex);
  }
}

Of course these are just two simple examples where, if an exception is thrown, I can log the method call and parameters passed in.

Is there another way to do this? Is there some way that I can extract the method being called and/or the parameters. I guess I'm hoping someone will tell me that I can just have some kind of function like:

LogError(ex);

And within that function I can access some Server or Environment variables that will expose the method being called. Maybe something like CurrentContext.WebServiceCall.Magic property... Do I need to wrap all my calls in a try/catch and then type out the method name and parameters, or is there another way to access this information.

Hopefully this question ins't too stupid.

A: 

Your LogError method could call Environment.StackTrace. Then it would "know" what method called it.

Keltex
+1  A: 

The exception object has a StackTrace property. Your entry method should be in the stack.

mark w
+1  A: 

Any unhandled exceptions will be bubbled up to your Application_Error event in the Global.asax file. From there, you can call Server.GetLastError() to retrieve the Exception instance.

Once you have the exception, you can look at the stack trace. You will also have access to the Request object so you can see exactly what came from the client.

Todd