I'm writing an audit logging system to be integrated into an existing piece of software, and the database structure includes a field for a blob of detail about the action being logged. Doesn't have to be anything fancy; it won't be needed much, and when it is, in most cases it'll be read by the devs to find out which method was called and with what parameters, stuff like that. This means that while it should probably be human-readable, it doesn't need to be parsed by anything. So, rather than have lots of overloads, or use horrible switch case string replacements, I figured a nice way to write the detail blob would be for a logging call to get the names of the parameters required by the action being logged, and the values that were passed to them. I've done some research, and found something promising in the reflection methods, but I've yet to find anything that explains clearly how to do it.
Here's a rough mock of what I'm after:
Dictionary<string,string> parameters = new Dictionary<string,string>();
foreach (Parameter p in MethodCall)
{
parameters.Add(p.Name, p.Value.ToString());
}
LogEvent(EventType.Something, userID = thisUser, details = parameters);
I understand it's likely to be more complex than that in practice, but it gives you the general idea. Inside my LogEvent method there's some stuff to add timestamps, construct descriptive sentences for the user-end log viewer (less technical detail than we show to admins and devs, so as not to expose our program/database structure) and - in theory - build some kind of blob (probably JSON or similar) from the Dictionary of parameter details.