patterncsharpMinor
Method/parameter tracer
Viewed 0 times
tracerparametermethod
Problem
I am writing a class which can log the activity of an application during run time. The plan is that the
What I need is the method name, the values of the parameters passed, and the output of the method (if specified). I also added a timer class to measure performance:
Usage:
```
class Program
{
static void Main(string[] args)
{
TestMethod("HELLO", "HI", 123);
SupportMI string will be sent to a DB, from where I can use some other code to format it properly for readability. I need this class to be usable with .Net-3.5 due to system limitations.What I need is the method name, the values of the parameters passed, and the output of the method (if specified). I also added a timer class to measure performance:
using System;
using System.Diagnostics;
namespace MyTimer
{
class MILogger : IDisposable
{
private string SupportMI;
private Stopwatch stopwatch = new Stopwatch();
private string[] parameters;
public static string methodoutput;
public MILogger()
{
stopwatch.Start();
}
public MILogger(params string[] args)
{
stopwatch.Start();
parameters = args;
}
public void Dispose()
{
stopwatch.Stop();
// New stacktrace
StackTrace stackTrace = new StackTrace();
// Method
var method = stackTrace.GetFrame(1).GetMethod();
string methname = method.Name;
SupportMI += methname + "(";
// Sorting parameters
for (int i = 0; i 0)
{
SupportMI += ",";
}
SupportMI += parameters[i];
}
// Timer, methooutput and some punctuation
SupportMI += ") { ";
SupportMI += methodoutput;
SupportMI += " TIMER: " + stopwatch.ElapsedMilliseconds.ToString();
SupportMI += "}";
}
public static void SendMI()
{
// Send SupportMI to a database
DBConnection db = new DBConnection(SupportMI);
}
}
}Usage:
```
class Program
{
static void Main(string[] args)
{
TestMethod("HELLO", "HI", 123);
Solution
Naming
Fields should be named using
If the field is
Using abbreviations for names is a no go as well, so
You could implement this like so
Fields should be named using
camelCase casing, so SupportMI -> supportMI. Abbreviations shouldn't be used for naming things, but I assume that MI has a special business meaning to you. If not, consider to rename this as well.If the field is
public one should use PascalCase casing for naming it. For compound words each "new" word will start with an uppercase letter , so methodoutput -> MethodOutput.Using abbreviations for names is a no go as well, so
and methname-> methodName.
General
By using the string.Join() method the "sorting of the parameter" can be improved a lot.
Instead of using string concatenation you should use a StringBuilder and its Append() method. This avoids the creation of a lot of string objects.
The StringBuilder's methods are implemented using a fluent interface which means that the methods return the StringBuilder's instance so one code for instance use it like stringBuilderObject.Append("someValue").Append("someOtherValue)...`You could implement this like so
var method = stackTrace.GetFrame(1).GetMethod();
var builder = new StringBuilder();
builder.Append(method.Name)
.Append("(")
.Append(string.Join(",", parameters))
.Append(") { ")
.Append(MethodOutput)
.Append(" TIMER: ")
.Append(stopwatch.ElapsedMilliseconds)
.Append("}");
supportMI = builder.ToString();Code Snippets
var method = stackTrace.GetFrame(1).GetMethod();
var builder = new StringBuilder();
builder.Append(method.Name)
.Append("(")
.Append(string.Join(",", parameters))
.Append(") { ")
.Append(MethodOutput)
.Append(" TIMER: ")
.Append(stopwatch.ElapsedMilliseconds)
.Append("}");
supportMI = builder.ToString();Context
StackExchange Code Review Q#119278, answer score: 4
Revisions (0)
No revisions yet.