Click here to Skip to main content
15,881,812 members
Articles / Programming Languages / C#

C#: Log All Parameters that Were Passed to a Method

Rate me:
Please Sign up or sign in to vote.
4.83/5 (11 votes)
13 Jul 2014CPOL1 min read 57.8K   465   16   7
Log all the parameters passed to some method in C#
In this article, you will see the Parameter Log utility class, which we can use to trace parameter values during an exception. We will see how to use the log utility, a Log string sample followed by a list of limitations that the logger has.

Introduction

Exceptions are common in any project. To track exceptions, we use error loggers which may only log the exception detail and some additional pieces of information. But hardly do we get an idea for which input set (parameters and values) a particular method is throwing the error.

Parameter Log Utility

Here is the utility class, which we can use to trace parameter values during the exception.

C#
internal class ParamLogUtility
{
    private readonly String _methodName;
    private String _paramaterLog;

    private readonly JavaScriptSerializer _serializer;
    private readonly Dictionary<String, Type> _methodParamaters;
    private readonly List<Tuple<String, Type, object>>_providedParametars;

    public ParamLogUtility(params Expression<Func<object>>[] providedParameters)
    {
        try
        {
            _serializer = new JavaScriptSerializer();
            var currentMethod = new StackTrace().GetFrame(1).GetMethod();

            /*Set class and current method info*/
            _methodName = String.Format("Class = {0}, Method = {1}", 
            currentMethod.DeclaringType.FullName, currentMethod.Name);
            
            /*Get current methods parameters*/
            _methodParamaters = new Dictionary<string, Type>();
            (from aParamater in currentMethod.GetParameters()
                select new { Name = aParamater.Name, DataType = aParamater.ParameterType })
                .ToList()
                .ForEach(obj => _methodParamaters.Add(obj.Name, obj.DataType));

            /*Get provided methods parameters*/
            _providedParametars = new List<Tuple<string, Type, object>>();
            foreach (var aExpression in providedParameters)
            {
                Expression bodyType = aExpression.Body;
                if (bodyType is MemberExpression)
                {                    
                    AddProvidedParamaterDetail((MemberExpression)aExpression.Body);
                }
                else if (bodyType is UnaryExpression)
                {
                    UnaryExpression unaryExpression = (UnaryExpression)aExpression.Body;
                    AddProvidedParamaterDetail((MemberExpression)unaryExpression.Operand);
                }
                else
                {
                    throw new Exception("Expression type unknown.");
                }
            }

            /*Process log for all method parameters*/
            ProcessLog();
        }
        catch (Exception exception)
        {
            throw new Exception("Error in paramater log processing.", exception);
        }
    }
    private void ProcessLog()
    {
        try
        {
            foreach (var aMethodParamater in _methodParamaters)
            {
                var aParameter =
                    _providedParametars.Where(
                        obj => obj.Item1.Equals(aMethodParamater.Key) && 
                obj.Item2 == aMethodParamater.Value).Single();
                _paramaterLog += String.Format(@" ""{0}"":{1},", 
            aParameter.Item1, _serializer.Serialize(aParameter.Item3));
            }
            _paramaterLog = (_paramaterLog != null) ? 
                             _paramaterLog.Trim(' ', ',') : string.Empty;
        }
        catch (Exception exception)
        {
            throw new Exception("MathodParamater is not found in providedParameters.");
        }
    }

    private void AddProvidedParamaterDetail(MemberExpression memberExpression)
    {
        ConstantExpression constantExpression = 
                           (ConstantExpression) memberExpression.Expression;
        var name = memberExpression.Member.Name;
        var value = ((FieldInfo) 
                      memberExpression.Member).GetValue(constantExpression.Value);
        var type = value.GetType();
        _providedParametars.Add(new Tuple<string, Type, object>(name, type, value));
    }

    public String GetLog()
    {
        return String.Format("{0}({1})", _methodName, _paramaterLog);
    }
}
  • Inside contractor, ParamLogUtility(params Expression<Func<object>>[] providedParameters) we will pass all the expected method parameters.
  • GetLog() will return us the error log string

Using Log Utility

To use the code, we may have to add System.Web.Extensions.dll reference at our project, which is located somewhere around this location

C:\Program Files 
(x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Web.Extensions.dll

If the method contains any parameter. we will use the utility class like:

C#
private void Add(string id, int age, string name, PersonEntity aPersonEntity)
{
    try
    {
        throw new NotImplementedException();
    }
    catch (Exception exception)
    {
        /*gives the log*/
        var paramLog = new ParamLogUtility(() => aPersonEntity, 
        () => age, () => id, () => name).GetLog();
    }
}

It is not important that all methods should have parameters. If there is no parameter, we can use it like:

C#
var paramLog = new ParamLogUtility().GetLog();

Log String Sample

C#
Class = MethodParamatersLog.Program, Method = Add("id":"1", 
"age":24, "name":"Dipon", 
"aPersonEntity":{"CreatedDateTime":"\/Date(1405115574273)\/",
"Id":"1","Name":"Dipon","Age":24})

Limitations

This logger does have some limitations like:

  1. Only parameters pacified at method signature could participate in log process. If we add some extra variables to the process, it would throw an error as here aPersonEntity is not part of the method signature.
    C#
    private static void Add(string id, int age, string name)
    {
        PersonEntity aPersonEntity = new PersonEntity();
        try
        {
            throw new NotImplementedException();
        }
        catch (Exception exception)
        {
            /*aPersonEntity would not take part in log process, 
              as it is not used in methods signature, and with throw error
            */
            var paramLog = new ParamLogUtility(() => aPersonEntity, () => age, 
            () => id, () => name).GetLog();
            var error = exception;
        }
    } 
  2. All the parameters of the method should take part in the log process. If not, it would throw an error. This is important because sometimes we may forget to add method parameters to the log string. This error will remind us to use thus unused parameters.
    C#
    private static void Add(string id, int age, string name)
    {
        PersonEntity aPersonEntity = new PersonEntity();
        try
        {
            throw new NotImplementedException();
        }
        catch (Exception exception)
        {
            /*did n't pointed all the parameters of the method(id, age)*/
            var paramLog = new ParamLogUtility(() => name).GetLog();
            var error = exception;
        }
    }
  3. There can be some other errors too, which I haven’t faced yet. So if you find any, please let me know.
  4. Should we use this utility to every method? Obviously not!

You can find the sample project of VS 2013 solution in the attachment.

History

  • 13th July, 2014: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Bangladesh Bangladesh
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionOne more issue Pin
MrBjorn7-May-20 4:31
MrBjorn7-May-20 4:31 
for instance:

private void SetLevelButtonClick(object sender, EventArgs e)

a usual method style in winforms. But here it fails match sender to actual parameter. Anything that can be done?
QuestionVery nice! One question: Pin
MrBjorn29-Apr-20 0:02
MrBjorn29-Apr-20 0:02 
QuestionVery nice Pin
sairamaj10-Oct-19 13:01
sairamaj10-Oct-19 13:01 
AnswerRe: Very nice Pin
DiponRoy3-Jul-20 3:18
DiponRoy3-Jul-20 3:18 
GeneralMy vote of 5 Pin
Carlos190714-Jul-14 0:34
professionalCarlos190714-Jul-14 0:34 
GeneralRe: My vote of 5 Pin
DiponRoy14-Jul-14 1:02
DiponRoy14-Jul-14 1:02 
GeneralRe: My vote of 5 Pin
Carlos190714-Jul-14 1:04
professionalCarlos190714-Jul-14 1:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.