Created
June 22, 2014 07:54
-
-
Save regme/1bb3d6ae5868ed582361 to your computer and use it in GitHub Desktop.
Log with params
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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