如何在配置文件中注册自定义Log4Net对象渲染器

时间:2010-11-11 12:34:14

标签: .net vb.net log4net log4net-configuration

我正在尝试使用自定义渲染器在log4net中记录对象。我的配置文件包含以下行:

  <renderer renderingClass="LogLibrary.Log4NetObjRenderers.PdaLogObjRenderer" renderedClass="LogLibrary.TranferObjects.PdaLogObj" />

但是我在加载配置文件时遇到了TypeLoadException,而log4net内部调试提供了这个细节:

log4net:ERROR OptionConverter: Could not instantiate class [LogLibrary.Log4NetObjRenderers.PdaLogObjRenderer].
System.TypeLoadException: Could not load type [LogLibrary.Log4NetObjRenderers.PdaLogObjRenderer]. Tried assembly [log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821] and all loaded assemblies
   at log4net.Util.SystemInfo.GetTypeFromString(Assembly relativeAssembly, String typeName, Boolean throwOnError, Boolean ignoreCase) in c:\work\svn_root\apache\log4net\tags\log4net-1.2.10-rc2\build\package\log4net-1.2.10\src\Util\SystemInfo.cs:line 671
   at log4net.Util.SystemInfo.GetTypeFromString(String typeName, Boolean throwOnError, Boolean ignoreCase) in c:\work\svn_root\apache\log4net\tags\log4net-1.2.10-rc2\build\package\log4net-1.2.10\src\Util\SystemInfo.cs:line 602
   at log4net.Util.OptionConverter.InstantiateByClassName(String className, Type superClass, Object defaultValue) in c:\work\svn_root\apache\log4net\tags\log4net-1.2.10-rc2\build\package\log4net-1.2.10\src\Util\OptionConverter.cs:line 477
log4net:ERROR XmlHierarchyConfigurator: Could not instantiate renderer [LogLibrary.Log4NetObjRenderers.PdaLogObjRenderer].

对象和渲染器的命名空间是正确的。

有什么想法吗?

编辑:这就是我在Global.asax

中设置log4net的方法
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    // Fires when the application is started

    Dim logfile As String = "C:\Applications\LoggerWebService\Config\logConfig.xml"
    log4net.GlobalContext.Properties("Application") = My.Application.Info.ProductName
    log4net.GlobalContext.Properties("Version") = My.Application.Info.Version.ToString

    log4net.Config.XmlConfigurator.ConfigureAndWatch(New IO.FileInfo(logfile))

    Dim logger As log4net.ILog = log4net.LogManager.GetLogger(GetType(Global_asax))
    logger.Info("Started")

End Sub

2 个答案:

答案 0 :(得分:2)

我无法想象这根本不起作用。尝试使用完全限定的类名。为此,您需要fully qualified assembly name

编辑: 对不起,我的意思是类名而不是程序集名称。根据您的评论,我假设您的案例中的完全限定类名将使配置如下所示:

 <renderer renderingClass="LogLibrary.Log4NetObjRenderers.PdaLogObjRenderer, LogLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
           renderedClass="LogLibrary.TranferObjects.PdaLogObj, LogLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />

答案 1 :(得分:1)

这是log4Net中的一个错误。如果您注释掉<renderer行,然后在Web服务运行时取消注释,它将会有效。

我能给出的唯一解释,我不知道我的假设是多么准确,但看起来好像并非所有的程序集都及时加载到log4Net来获取渲染器......