0

How do I know the log the last property that is null?

For example,

var a = "somevalue";
......
......
if(a == null)
{
   Log.Error(MethodBase.GetCurrentMethod().Name  + "Property : a is null");      

   //blah blah
}

Like how I use the reflection to get the current method name, there should be some means by which I can log the latest local variables (or a property or fields) that is being compared ? I use, log4net by the way to log the errors.

1) Is there any method to achieve this or should we manually log it?

2) Is there any custom method that prints the class -> MethodName -> Propertyname(or FieldName) that is null?

Thanks for your time in advance.

1 Answers1

2

As mentioned by @fsimonazzi, "a" would be a local variable.

That being said there is still no way to examine the current compare operation as in MSIL there is no formal concept of an IF block - only conditional jumps.

If you wanted to get really crazy with the reflection, you may be able to find the current executing instruction and look around near that for a variable, but even then, you will not find the name - only a reference - as names are only used prior to compilation.

Either way, reflection is not going to help you here.

Instead, try using Exceptions - specifically ArgumentNullException. This body of code would become:

void doStuff(string param1, int param2)
{
    if (param == null)
        throw new ArgumentNullException("param1", "param1 must not be null");
    if (param2 < 0)
        throw new ArgumentOutOfRangeException("param2", "param2 should be non-negative.");

    //method body
}

then, when you call the method, you can catch the exception and log it - no matter what it may be.

public static void Main(string[] args)
{
    try 
    {
        doStuff(null, 3);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
}

Tools like FxCop can help make sure that you are properly validating each parameter.


Properties are actually implemented as methods, so reflection could help you there. If, for example, you were validating in a property and wanted to log the position automatically, you could.

private object _cachedObject = null;
public object CachedObject 
{
    get 
    {
        if (_cachedObject == null)
        {
            log(MethodBase.GetCurrentMethod().Name, "creating cached object");
            _cachedObject = createCachedObject();
        }
        return _cachedObject;
    }
}

The .Net Framework 4.5 also brings with it a new attribute that can be used to replace the MethodBase.GetCurrentMethod().Name construct you are using to get the method name. See [CallerMemberNameAttribute][3].

Mitch
  • 21,223
  • 6
  • 63
  • 86
  • And GetCurrentMethod is not reliable in general. http://stackoverflow.com/questions/8902662/when-is-methodbase-getcurrentmethod-reliable-predictable. If you're using .NET 4.5 you could use the new CallerMemberAttribute http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.callermembernameattribute.aspx and move the logging to a separate method, so you don't have to hard-code the method name but you don't also need to rely on reflection. – fsimonazzi Nov 21 '12 at 16:11
  • As u pointed out, many say, to avoid reflection. anyways, thanks for the links. Checking out now. – now he who must not be named. Nov 21 '12 at 16:13
  • @mitch :Is checking the stackframe and getting the methodname from it is Ok? – now he who must not be named. Nov 21 '12 at 16:15
  • @nowhewhomustnotbenamed., it is not illegal or impossible, but it may not suit your needs. Compiler optimizations may restructure your code such that the value returned may not match what you expect. Reflection is also somewhat costly relative to a constant string, but I don't know your use case. If it works for you, and is not causing a problem, then it is Ok. I would still consider CallerMemberNameAttribute, however. – Mitch Nov 21 '12 at 16:20
  • Thanks for your help Mitch. Will research. – now he who must not be named. Nov 21 '12 at 16:23