我为对象设置了IObjectRenderer
,但在记录
使用Log4Net调试我可以看到渲染器已加载并找到了要渲染的正确对象类型,但它似乎忽略了它。
输出:
log4net: Rendering class [ThirdPartyApi.Code.Logging.ModelStateRenderer, ThirdPartyApi], Rendered class [System.Web.Http.ModelBinding.ModelStateDictionary].
log4net: Loaded type [System.Web.Http.ModelBinding.ModelStateDictionary] from assembly [System.Web.Http, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35] by searching loaded assemblies.
日志输出:
28927 [10]() DEBUG ThirdPartyApi.Controllers.AuthenticationController (null) Debug - Model Validation: System.Web.Http.ModelBinding.ModelStateDictionary
渲染器类:
public class ModelStateRenderer : IObjectRenderer
{
public void RenderObject(RendererMap rendererMap, object obj, TextWriter writer)
{
var modelStateDict = obj as ModelStateDictionary;
if (modelStateDict != null)
{
foreach (var modelState in modelStateDict)
{
writer.Write("Model: {0}, Value: {1}\n", modelState.Key, modelState.Value);
if (modelState.Value.Errors != null && modelState.Value.Errors.Any())
{
foreach (var error in modelState.Value.Errors)
{
writer.Write("Error Mssage: {0}\n Exception: {1}\n", error.ErrorMessage, error.Exception);
}
}
}
}
}
}
如果我断点上面的方法它永远不会被击中,所以它似乎根本不使用渲染器
配置:
<renderer renderingClass="ThirdPartyApi.Code.Logging.ModelStateRenderer, ThirdPartyApi" renderedClass="System.Web.Http.ModelBinding.ModelStateDictionary" />
有人有什么想法吗?
可能必须将日志记录添加到log4net以查找为什么log4net没有记录它应该记录的内容:)
答案 0 :(得分:3)
log4net RendererMap.FindAndRender()方法是确定需要使用哪个渲染器的方法。它看起来像这样;
public void FindAndRender(object obj, TextWriter writer)
{
if (obj == null)
{
writer.Write(SystemInfo.NullText); // <=== here!!!
}
else
{
// etc..
}
}
这解释了为什么在日志输出中看到(null)
的原因。或者换句话说,您尝试记录的对象是 null 。所以当然你的渲染器没有被调用。