Hello,
as "exercise" (I needed it) I've built a simple logging/tracing classLibrary and I'm using it to write in a txt what my application is doing, and save the exceptions.
I'm not an expert programmer and I want to improve my library. Actually to trace something I have to do like this:
Now, I'd like to add time measurement in my log. I've found an interesting class to do that, the Stopwatch one (using System.Diagnost ics).
I can do that as follows:
Now, I'd like to add a new method in my classLibrary, and call it let's say MeasureAndTrace (string methodToCall, string logMessage);
The tricky thing is the "methodToCa ll". I'm not even sure it's correct to use it as string. My idea is to use the method to launch a method and measure the time, as done before with the Stopwatch, as follows:
And this is how the MeasureAndTrace would look like, in my library.
My question is, can you help me to correctly adapt my MeasureAndTrace method in my logging classLibrary and tell me what do add in the line "//Here I need the code to launch the methodToCall defined as parameter"?
Note: methodToCall could be something more complex, e.g. a method containing some parameters...
Another note: what happens if the method that I want to pass as methodToCall would return a value, would it be possible to still call it, e.g. like this (in case it returns a string)?
I've already googled for: c# function name parameter and read some suggestions by people to use delegates, or reflection. I'm reading some tutorials about delegates but actually I can't understand how to use them in my practical example above mentioned. If you can give me some code, I would really appreciate.
Thanks a lot,
cheers,
--firepol
as "exercise" (I needed it) I've built a simple logging/tracing classLibrary and I'm using it to write in a txt what my application is doing, and save the exceptions.
I'm not an expert programmer and I want to improve my library. Actually to trace something I have to do like this:
Code:
Logger log = new Logger();
SomeMehod();
log.Trace("I usually write a comment here, such as: SomeMehod()");
I can do that as follows:
Code:
Logger log = new Logger();
Stopwatch watch = new Stopwatch();
watch.Start();
SomeMethod();
watch.Stop();
log.Trace("SomeMehod() time: " + watch.Elapsed.ToString() );
The tricky thing is the "methodToCa ll". I'm not even sure it's correct to use it as string. My idea is to use the method to launch a method and measure the time, as done before with the Stopwatch, as follows:
Code:
MeasureAndTrace("SomeMethod()", "SomeMethod launched");
Code:
MeasureAndTrace(string methodToCall, string logMessage)
{
Logger log = new Logger();
Stopwatch watch = new Stopwatch();
watch.Start();
// PLEASE LOOK HERE
//Here I need the code to launch the methodToCall defined as parameter
watch.Stop();
log.Trace("SomeMehod() time: " + watch.Elapsed.ToString() );
}
Note: methodToCall could be something more complex, e.g. a method containing some parameters...
Another note: what happens if the method that I want to pass as methodToCall would return a value, would it be possible to still call it, e.g. like this (in case it returns a string)?
Code:
string resultString = MeasureAndTrace(string methodToCall, string logMessage);
Thanks a lot,
cheers,
--firepol
Comment