This question is related to another question I asked on this site but I think I have to post a new question to get an answer. That question is: How do I get the values of the properties of a model object in ASP.NET MVC when passed in as an object?
I would like to add a method to each of the model classes that were generated by MVC for the database tables. I want to be able to write information to a log about any object from the model by just calling some method on each object. It could be .LogInfo and would return a string. Currently I am using JSON serialization in my logging method and I pass in whatever object I want to log about. I have to knock out certain fields with the [JsonIgnore] attribute to prevent sensitive information being written to the log. It would be better to have to affirmatively add each field I want to log to some method.
I feel like I should be extending a class that contains an abstract .LogInfo method that each class from my model should extend. But I don't think I can do that, even with partial classes.
Is there some way this can be done with some facility I don't know about? If I add the .LogInfo method to each model class, then it would be wiped out if I have to regenerate the model. Maybe this is acceptable, but I am wondering if there is a more proper way.
This is how I cam declaring my LogEvent method, so I can pass in any object from the model:
public void LogEvent(string userName, int? userID, int? townID, string httpVerb, string description, bool isError, Object obj)
{
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.MaxDepth = 1;
settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
settings.NullValueHandling = NullValueHandling.Ignore;
string bigText = JsonConvert.SerializeObject(obj, settings);
LogEvent(userName, userID, townID, httpVerb, description, isError, bigText);
}
because I have another overloaded method that takes the last parameter as a string and writes it to the log.
but I would rather be doing this:
public void LogEvent(string userName, int? userID, int? townID, string httpVerb, string description, bool isError, Object obj)
{
LogEvent(userName, userID, townID, httpVerb, description, isError, obj.LogInfo());
}