我的代码如下:
//Validate that all the attributes are well setted
ValidationContext vc = new ValidationContext(callDAO, null, null);
var resultsValidation = new List<ValidationResult>();
bool validated = Validator.TryValidateObject(callDAO, vc, resultsValidation, true);
if(validated)
{
//Execute some code
}
else{
notValidated++;
var errorValidations = resultsValidation.Select(v => v.ErrorMessage);
log.Error(string.Format("The attribute VALIDATION FAILED due to: " + errorValidations));
}
我想要做的是正确打印日志文件中“errorValidations”中包含的错误。但是在日志中写入时,会写入以下内容:
System.Linq.Enumerable+WhereSelectListIterator`2[System.ComponentModel.DataAnnotations.ValidationResult,System.String]
问题:如何正确显示“resultsValidation”中包含的信息?
注意:当引入的attribut lenght大于定义时,“resultsValidation”中的内容示例:
[0] {The field Id must be a string with a maximum lenght of 15.}
谢谢大家的回答。
答案 0 :(得分:0)
errorValidations
属于IEnumerable<ValidationResult>
类型,您需要一个foreach
循环来遍历集合以打印每个验证结果,
foreach (ValidationResult result in errorValidations)
log.Error("The attribute VALIDATION FAILED due to: " + result.ToString());