How to Configure Log4Net Custom Object Renderer for Generic Objects?

我的梦境 提交于 2020-01-05 02:29:48

问题


I have a generic object: MyGenericObject(of T), how do I register a custom renderer in log4net in the config file? For example, if this was a normal object the following would work:

<renderer renderingClass="MyObjectRenderer, MyClassLibrary" renderedClass="MyObject, MyClassLibrary" />

However I don't know how to write this for Generics, I attempted this:

<renderer renderingClass="MyObjectRenderer, MyClassLibrary" renderedClass="MyObject(Of MyNonGenericObject), MyClassLibrary" />

but this happened:

log4net:ERROR XmlHierarchyConfigurator: Could not find class [MyObject(Of MyNonGenericObject), MyClassLibrary].
System.TypeLoadException: Could not load type 'MyObject(Of MyNonGenericObject)' from assembly 'MyClassLibrary'.

Does anyone have any better ideas on how to achive what I want?


回答1:


The syntax you are using in XML file, MyObject(Of MyNonGenericObject), is specific to VB .NET, just like MyObject<MyNonGenericObject> is specific to C#.

You change your declaration to use language-independent CLR type syntax:

MyObject`1[[MyNonGenericObject, MyClassLibrary]], MyClassLibrary

(1 after ` indicates the number of type parameters.)

So the line will be:

<renderer renderingClass="MyObjectRenderer, MyClassLibrary" renderedClass="MyObject`1[[MyNonGenericObject, MyClassLibrary]], MyClassLibrary" />


来源:https://stackoverflow.com/questions/6844163/how-to-configure-log4net-custom-object-renderer-for-generic-objects

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!