Typically my repositories have logging statements for debugging purposes, allowing me to see values of parameters. Recently I went down the path of creating a generic repository that take predicate Expression as argument for great flexibility, however I cannot figure out a decent way of logging the condition to where it would be remotely useful.
Example Method:
public int GetCount<K>(Expression<Func<K, bool>> predicate) where K : class
{
Logger.LogDebugMessage(String.Format("Parameters [predicate: {0}]", predicate == null ? string.Empty : predicate.Body.ToString()));
...
}
You can see I am using the Body.ToString() at this point, but the results are not-so-readble:
Parameters [predicate: (fa.SomeId == value(NameSpace.SomeClass+<>c__DisplayClass2).SomeId)]
Ultimately what I would love to see is something similar to the below:
Parameters [predicate: (fa.SomeId == 1 && fa.Account.Name == "MyName").SomeId)]
In essence the value of this log is being able to know the input values when something blows up. Is there any way around this short of forcing the user of the API to supply the predicate as a string?
==
expression and replacing it with its value, but that would not correctly represent the actual predicate. – Fogboundx.Something == ExpressionNotInvolvingXYouWantToEvaluateEagerly
? If so, it's easy to simplify. – Fogbound