Skip to content

Instantly share code, notes, and snippets.

@regme
Created June 22, 2014 07:54
Show Gist options
  • Save regme/1bb3d6ae5868ed582361 to your computer and use it in GitHub Desktop.
Save regme/1bb3d6ae5868ed582361 to your computer and use it in GitHub Desktop.
Log with params
private static void MyMethod(string s, int x, int y)
{
try
{
throw new NotImplementedException();
}
catch (Exception ex)
{
LogError(MethodBase.GetCurrentMethod(), ex, s, x, y);
}
}
private static void LogError(MethodBase method, Exception ex, params object[] values)
{
ParameterInfo[] parms = method.GetParameters();
object[] namevalues = new object[2 * parms.Length];
string msg = "Error in " + method.Name + "(";
for (int i = 0, j = 0; i < parms.Length; i++, j += 2)
{
msg += "{" + j + "}={" + (j + 1) + "}, ";
namevalues[j] = parms[i].Name;
if (i < values.Length) namevalues[j + 1] = values[i];
}
msg += "exception=" + ex.Message + ")";
Console.WriteLine(string.Format(msg, namevalues));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment