I am trying to create a web service to handle all logging for application asynchronous, where I can send two of the same object and compare each property. Then create of log of property that changed.
The issue i am having, i don't know how can the apicontroller accept any object:
This is i have so far:
public class PropertyLogApiCall
{
public string Id { get; set; }
public string Username { get; set; }
public string Task { get; set; }
public object OldEntity { get; set; }
public object NewEntity { get; set; }
public string OldEntityType { get; set; }
public string NewEntityType { get; set; }
}
POST - ApiController
public void Post(PropertyLogApiCall paramList)
{
try
{
var id = paramList.Id;
var username = paramList.Username;
var task = paramList.Task;
var newType = Type.GetType(paramList.NewEntityType);
var oldType = Type.GetType(paramList.OldEntityType);
var newEntity = Convert.ChangeType(paramList.NewEntity, newType);
var oldEntity = Convert.ChangeType(paramList.OldEntity, oldType);
var properties = oldEntity.GetType().GetProperties();
var logsToSave = new List<PropertyLog>();
var dateTimeStamp = DateTime.Now;
foreach (var property in properties)
{
var oldValue = property.GetValue(oldEntity, null);
var newValue = newEntity.GetType().GetProperty(property.Name).GetValue(newEntity, null);
var propertyType = property.PropertyType;
var name = oldEntity.GetType().Name;
var propName = property.Name;
PropertyLog log = null;
if (propertyType == typeof(string))
{
log = CreateLogString(oldValue, newValue);
}
else if (propertyType == typeof(int))
{
log = CreateLogInt(oldValue, newValue);
}
if (log != null)
{
log.Created = dateTimeStamp;
log.EntityId = id;
log.Username = username;
log.EntityName = name;
log.Property = propName;
log.Task = task;
logsToSave.Add(log);
}
}
//Save Logs
.....
}
catch (Exception e)
{
//send email
}
This doesn't work because most of the time newType/oldType is null. And some entity don't implement the IConvertible interface.
Is this even possible to do?