Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic logging of function parameters in exception handling

Tags:

c#

.net

A lot of my C# code follows this pattern:

void foo(string param1, string param2, string param3) {     try     {          // do something...     }     catch(Exception ex)     {         LogError(String.Format("Error in foo(param1={0}, param2={1}, param3={2}), exception={3}", param1, param2, param3, ex.Message));     } } 

Is there a way in .NET to get a Key/Value list of the parameters to a function so that I can call another function to construct my error logging string? OR Do you have a more generic / better way of doing this?

like image 386
Guy Avatar asked Sep 25 '08 20:09

Guy


People also ask

What is exception logging?

Logging an exception in python with an error can be done in the logging. exception() method. This function logs a message with level ERROR on this logger.

What is logging exception in asp net?

ELMAH (Error Logging Modules and Handlers) is an error logging facility that you plug into your ASP.NET application as a NuGet package. ELMAH provides the following capabilities: Logging of unhandled exceptions. A web page to view the entire log of recoded unhandled exceptions.

How do you log an exception in Java?

Starting with Java 7, you can catch multiple exception types in a single block by separating the exception type with a vertical bar ( | ). While it's still recommended to catch unique exceptions per catch block, this lets you simplify your code. You should also consider using the Thread.

What is logging exception in C#?

The Anatomy of C# Exceptions catch – When an exception occurs, the Catch block of code is executed. This is where you are able to handle the exception, log it, or ignore it. finally – The finally block allows you to execute certain code if an exception is thrown or not.


2 Answers

You could use Reflection and the convention that you must pass the parameters to the LogError with the right order:

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)); } 
like image 120
Panos Avatar answered Sep 16 '22 14:09

Panos


You could use aspect oriented programming with PostSharp (have a look at http://www.postsharp.org, and the tutorial at http://www.codeproject.com/KB/cs/ps-custom-attributes-1.aspx). Basically you could do something like this:

public class LogExceptionAttribute : OnExceptionAspect {  public override void OnException(MethodExecutionEventArgs eventArgs)  {   log.error("Exception occurred in method {0}", eventArgs);   } }  [LoggingOnExceptionAspect] public foo(int number, string word, Person customer) {    // ... something here throws an exception } 

Perhaps not quite what you want, but I'm sure it can be adapted to suit your needs.

like image 43
Jacob Avatar answered Sep 17 '22 14:09

Jacob