ASP.Net MVC 學習筆記
MVC的LOG 機制如何將 Entity的ErrorMessage取得
string sMsg = string.Empty;
Exception exception = args.Exception;
var validationException = exception as DbEntityValidationException;
//validationException.EntityValidationErrors
foreach (var a in validationException.EntityValidationErrors)
{
foreach (var b in a.ValidationErrors)
sMsg += b.ErrorMessage;
}
var Exception = args.Exception.GetBaseException().GetBaseException();
GlobalDiagnosticsContext.Set("LogStackTrace", Exception.StackTrace);
logger.Error(Exception.Message + sMsg);
//清空制定NLOG欄位的值
GlobalDiagnosticsContext.Clear();