2

I'm trying to log an object in log4net using a custom renderer. My configuration file has the following line:

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

But I get a TypeLoadException when loading the configuration file, and log4net internal debugging gives this detail:

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].

The Namespace of the object and the renderers are correct.

Any ideas?

EDIT: This is how I am setting log4net up in Global.asax

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
Mr Shoubs
  • 14,629
  • 17
  • 68
  • 107

2 Answers2

2

I cannot imagine that this is not working at all. Try to use the fully qualified class name. For this you need the fully qualified assembly name.

EDIT: Sorry, I meant class name not assembly name. Based on your comment I assume the fully qualified class name in your case would make the configuration look like this:

 <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" />
Community
  • 1
  • 1
Stefan Egli
  • 17,398
  • 3
  • 54
  • 75
  • Sounded like a good idea until I tried - This gives me "LogLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" which doesn't help at all. - I need to put the type here, not the assembly name as per instructions here: http://logging.apache.org/log4net/release/manual/configuration.html#Renderers – Mr Shoubs Nov 11 '10 at 15:38
  • note...I am surprised it doesn't work either - I'm sure it did when I did this before. I sometimes works without modifying the app whilst it is running, but this happens rarely. I'm using a web service and the renderer is for an object in another class library. It is certainly strange behaviour - it is replaceable in my dev environment and the test box running IIS7. – Mr Shoubs Nov 11 '10 at 15:46
  • Sorry, I was not precise enough. Cf. my revised answer. – Stefan Egli Nov 11 '10 at 16:32
  • That does seemed to have worked, though it isn't a great solution as you need to include the version in there - I don't want to have to maintain the log config every time I update the webservice. Do you have any idea why it isn't working as per the documentation? – Mr Shoubs Nov 11 '10 at 16:47
  • I played around with this and it works with the following: ... I still don't understand why it can't find it as per log4net doc. – Mr Shoubs Nov 11 '10 at 17:10
  • 1
    This is probably a .Net problem. It cannot tell based on the class name what the assembly would be therefore it fails to load the type if the assembly is not already loaded. With your solution you give that missing information; my version is probably overkill in this situation... – Stefan Egli Nov 11 '10 at 18:03
  • Thanks, makes sense. It strikes me as strange thought that there wasn't much any information on this that I could find from google. oh well. – Mr Shoubs Nov 12 '10 at 10:06
1

This is a bug in log4Net. If you comment out the <renderer line and then uncomment it whilst the web service is running, it will work.

The only explanation I can give, and I don't know how accurate my assumption is, but it looks as though not all the assemblies are loaded in time for log4Net to get the renderer...

Mr Shoubs
  • 14,629
  • 17
  • 68
  • 107
  • I've submitted a bug report - though it doesn't look like they work through them very often: https://issues.apache.org/jira/browse/LOG4NET-277 – Mr Shoubs Nov 11 '10 at 13:32