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:
Expand|Select|Wrap|Line Numbers
- Logger log = new Logger();
- SomeMehod();
- log.Trace("I usually write a comment here, such as: SomeMehod()");
I can do that as follows:
Expand|Select|Wrap|Line Numbers
- 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 "methodToCall". 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:
Expand|Select|Wrap|Line Numbers
- MeasureAndTrace("SomeMethod()", "SomeMethod launched");
Expand|Select|Wrap|Line Numbers
- 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)?
Expand|Select|Wrap|Line Numbers
- string resultString = MeasureAndTrace(string methodToCall, string logMessage);
Thanks a lot,
cheers,
--firepol