For couple of months I work on my Ultimate Workflow Toolkit. For those who doesn’t know what is it (especially for power users and consultants who doesn’t write code but like build and use workflows) – it is a toolkit that contains steps for workflows to extend list of basic operations. To make troubleshooting of issues you can use tracing and there are many article that describe how to use it, how to log inputs of plugins but I haven’t found any article that describes how to log all inputs of generic custom workflow activity without usage of property names and types directly. So I developed code that makes your life easier and logs all the inputs for you:
var properties = GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); properties.ToList().ForEach(p => { if (p.PropertyType.IsSubclassOf(typeof(InArgument)) || p.PropertyType.IsSubclassOf(typeof(InOutArgument))) { var propertyLabel = ((InputAttribute) p.GetCustomAttribute(typeof(InputAttribute))).Name; var logText = $"Value of '{propertyLabel}' attribute equals to "; var property = (Argument) p.GetValue(this); var propertyValue = property.Get(executionContext); if (propertyValue == null) logText += "empty"; else if (propertyValue is string || propertyValue is decimal || propertyValue is int || propertyValue is bool) logText += propertyValue.ToString(); else if (propertyValue is DateTime) logText += ((DateTime) propertyValue).ToString("yyyy-MM-dd HH:mm:ss \"GMT\"zzz"); else if (propertyValue is EntityReference) { var er = (EntityReference) propertyValue; logText += $"Id: {er.Id}, LogicalName: {er.LogicalName}"; } else if (propertyValue is OptionSetValue) logText += ((OptionSetValue) propertyValue).Value; else if (propertyValue is Money) logText += ((Money) propertyValue).Value.ToString(); else logText += $"undefined type - {p.GetType().FullName}"; tracingService.Trace(logText); } });
I’m appreciated to Zooy for the basic idea.
1 Comment