0

For Example In Javascript:

function logger(args) {
  console.log('Func "' + args.callee.name + '" invoked.');
  for (var arg in args) {
    console.log(arg); //well I cannot get those arguments' name in js
  }
}

function doSomething(x, y, z) {
  logger(this.arguments);
  //do something...
}

How can I do something similar to that in C#?

Actually, I was about to implement a Web Service Logger in my program. Any suggestion for that?

Thanks to all.

edited: Sorry I didn't make it clearer. I knew that System.Reflection.MethodBase.GetCurrentMethod() could get me the caller function's MethodInfo, but the arguments' value is what I concerned more.

Narcotics
  • 313
  • 1
  • 8
  • System.Reflection. Read up about it, pretty useful for a situation like yours. –  Jun 30 '15 at 06:37
  • what is your web service framework? wcf? – Tolga Evcimen Jun 30 '15 at 06:37
  • @NahumLitvin, I read that before, but seems like it cannot get the arguments **value**, or maybe I just missed something? – Narcotics Jun 30 '15 at 06:44
  • @TolgaEvcimen, asmx webservice, if it was a web api project, maybe life would be easier :( – Narcotics Jun 30 '15 at 06:45
  • I was thinking about the WCF actually. There are many oportunuties in wcf. For the method name you can easily use reflection as others told. But for parameters you need some kind of an interception mechanism. I don't know if it is possible with asmx webservices. But there should be ways. – Tolga Evcimen Jun 30 '15 at 06:51

2 Answers2

0

method name:

void logger([CallerMemberName] string methodName = "") { ... }

when you call logger() in doSomething(), methodName should be 'doSomething'.

but is no way to get arguments.

Cologler
  • 724
  • 6
  • 18
0

see:

Can I get parameter names/values procedurally from the currently executing function?

you basicly cannot do this. unless you use AOP tool like PostSharp

Community
  • 1
  • 1
Nahum
  • 6,959
  • 12
  • 48
  • 69
  • Thanks for that, I was looking for a more simple and light-weight solution before. But I'll dig into the `PostSharp` later, see if I can use it easily. – Narcotics Jun 30 '15 at 07:01