Support variables to be added to the time attribute
GeertvanHorrik opened this issue · 7 comments
For example, we have something like this:
[Time("{filename}")
public async Task ReadFileAsync(string filename)
{
// todo
}
This would only accept parameters inside the method to keep it a bit "simple". Then we can choose to:
- Specify a string
- Provide a dictionary (but don't want to go here since this will result in a lot of allocations)
If the Time format is not null or empty, we will provide the string.Format method. This way it will only "affect performance" when users actually use this feature.
What do you think?
I would accep a PR for this ;)
It's on the list :-)
I have a few more obligations, but once I have time I will definitely PR this
Started development, not sure how long it will take but will post progress here.
@SimonCropp We probably need a new method on the MethodTimeLogger. We can do 2 things:
- Add an additional one (overload)
- Replace (mark obsolete)
I am thinking of an overload like this (with a pre-formatted message):
public static void Log(MethodBase methodBase, long milliseconds, string message = null)
We could of course provide a dictionary as well but that will result in lots of allocations as discussed before. What are you thoughts?
We could actually support both and raise an error if people use this new feature. In that case they will need to add the message parameter at the end.
Docs that explain the specification:
Using parameters inside the logging
If you want to get the parameter values inside the logging, you can use a string format in the attribute definition.
public class MyClass
{
[Time("File name: '{fileName}'"]
public void MyMethod(string fileName)
{
//Some code u are curious how long it takes
Console.WriteLine("Hello");
}
}
Then this will be compiled
public class MyClass
{
public void MyMethod(string fileName)
{
var stopwatch = Stopwatch.StartNew();
try
{
//Some code u are curious how long it takes
Console.WriteLine("Hello");
}
finally
{
stopwatch.Stop();
var message = string.Format("File name: '{0}'", fileName);
MethodTimeLogger.Log(methodof(MyClass.MyMethod), stopwatch.ElapsedMilliseconds, message);
}
}
}
Note that this feature requires an updated Log method call with the definition below. If this method (with the message parameter) is not found, the weaver will raise an error.
public static void Log(MethodBase methodBase, long milliseconds, string message = null)
Further status updates will be posted in #33