467,892 Members | 1,855 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,892 developers. It's quick & easy.

Logging runtime values passed to a function

Hi, I want to write down a function entry point logging method, allowing
logging of passed in parameters values along with function name and parameter
names. I know I can get the parameter name and function name using
System.Reflection or StackFrame but not sure if if it's possible to retreive
runtime passed in actual parameter values in .net 1.1 ?

e.g.
class User
{
public string GetName(int userId)
{
//Logging function, which logs function name and parameters values e.g. in
this case GetName and whatever calling code has passed in for userId

rest of function code

}
Mar 27 '06 #1
  • viewed: 3671
Share:
2 Replies
>but not sure if if it's possible to retreive
runtime passed in actual parameter values in .net 1.1 ?


Not without explicitly naming them.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mar 27 '06 #2
thanks Mattias, I am using params object[] to dynamically log runtime value.
Following is the sample code. Please let me know if you think any other
better approach to solve this issue. Arif

////////////////////Sample code/////////////////////////////////
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//Sample One
int val = 10;
string name = "Arif";
BusinessFunction1(val,name);

//Sampe Two
int b = 10;
string name2 ="Test";
bool check = true;
BusinessFunction2(b,name2,check);
}

static void BusinessFunction1(int val, string name)
{
LogTrace(val,name);
}
static void BusinessFunction2(int b, string name2, bool check)
{
LogTrace(b,name2,check);
}

public static void LogTrace(params object[] args)
{
StackTrace callStack = new StackTrace(1, true);
StackFrame callingMethodFrame = callStack.GetFrame(0);

if (args == null || args.Length == 0)
{
Console.WriteLine("no parameters");
}
else
{
MethodBase callingMethod = callingMethodFrame.GetMethod();
ParameterInfo[] parameters = callingMethod.GetParameters();

int inParamCount = parameters.Length;
foreach (ParameterInfo parameter in parameters)
if (parameter.IsOut) inParamCount--;

Debug.Assert(inParamCount == args.Length, String.Format("Incorrect
number of arguments. Expected {0} but was {1}.", parameters.Length,
args.Length));

int paramCount = parameters.Length;
int argIndex = 0;
for (int i = 0; i < paramCount; i++)
{
Console.WriteLine(parameters[i].Name);
if (parameters[i].IsOut)
Console.WriteLine("<Unknown>");
else
{
if (args[argIndex] == null)
Console.WriteLine("<null>");
else
{
Console.WriteLine(args[argIndex].ToString().Trim());
}
argIndex++;
}
if (i < paramCount - 1)
Console.WriteLine(",");
}
}
}
}

"Mattias Sjögren" wrote:
but not sure if if it's possible to retreive
runtime passed in actual parameter values in .net 1.1 ?


Not without explicitly naming them.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Mar 28 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by domeceo | last post: by
16 posts views Thread by Einar Høst | last post: by
2 posts views Thread by Vinay Sajip | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.