To use log4net custom appender, config change only?

Wrote WCF-based appender. Mgmt wants no VS proj change

Page 1 of 1

6 Replies - 9809 Views - Last Post: 21 June 2009 - 08:59 PM Rate Topic: -----

#1 tjwin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-June 09

To use log4net custom appender, config change only?

Post icon  Posted 06 June 2009 - 09:29 AM

I wrote a custom log4net appender that sends over WCF to a web-based WCF service - going to extend to allow REST'ful verbs also. We currently use log4net with file and console appenders. Anyway, at end of testing and now am told that they(dev mgmt) don't want devs to have to add reference to their Visual Studio projects to use my appender - just change the app.config file. I have a test app that, of course, has a reference to log4net as well as my WCF-based appender. The app writes to all three appenders. When I take out the reference in VS to my appender but still in app.config as the log4net docs state, my appender is not called.
Does anyone know if log4net should/can call/use my appender by just a config file entry?

Is This A Good Question/Topic? 0
  • +

Replies To: To use log4net custom appender, config change only?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: To use log4net custom appender, config change only?

Posted 07 June 2009 - 07:34 AM

Looks like it's an FAQ:

log4net FAQ said:

Can I write a custom appender?

Yes. You can implement the log4net.Appender.IAppender interface to create you own customized appender. We recommend that you extend the log4net.Appender.AppenderSkeleton class rather than starting from scratch. You should implement your custom code in a assembly separate from the log4net assembly. To get started it is worth looking at the source of the log4net.Appender.TraceAppender as an example of the minimum amount of code required to get an appender working.

To configure log4net to use your custom appender you need to specify the assembly qualified name of the appender type in the config file. For example:

<appender name="..." type="MyNamespace.MyAppender, MyAssembly">

The .NET runtime will try to locate the assembly called MyAssembly. How .NET locates assemblies is beyond the scope of this FAQ.

Was This Post Helpful? 0
  • +
  • -

#3 tjwin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-June 09

Re: To use log4net custom appender, config change only?

Posted 07 June 2009 - 07:44 AM

Thanks for the reply. Yes, I originally read that. That's why I'm confused that my appender is called when a reference to it is added but is NOT called without the reference even though the config file has this entry as required.


View PostJackOfAllTrades, on 7 Jun, 2009 - 06:34 AM, said:

Looks like it's an FAQ:

log4net FAQ said:

Can I write a custom appender?

Yes. You can implement the log4net.Appender.IAppender interface to create you own customized appender. We recommend that you extend the log4net.Appender.AppenderSkeleton class rather than starting from scratch. You should implement your custom code in a assembly separate from the log4net assembly. To get started it is worth looking at the source of the log4net.Appender.TraceAppender as an example of the minimum amount of code required to get an appender working.

To configure log4net to use your custom appender you need to specify the assembly qualified name of the appender type in the config file. For example:

<appender name="..." type="MyNamespace.MyAppender, MyAssembly">

The .NET runtime will try to locate the assembly called MyAssembly. How .NET locates assemblies is beyond the scope of this FAQ.

Was This Post Helpful? 0
  • +
  • -

#4 masteryee  Icon User is offline

  • D.I.C Regular

Reputation: 40
  • View blog
  • Posts: 271
  • Joined: 16-May 09

Re: To use log4net custom appender, config change only?

Posted 08 June 2009 - 09:04 AM

Have you tried using the appender-ref tag?

<log4net>
    <appender name="Appender1" />
    <appender name="Appender2" />
    <root>
        <appender-ref ref="Appender1" />
        <appender-ref ref="Appender2"/>
    </root>
</log4net>


Was This Post Helpful? 0
  • +
  • -

#5 tjwin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-June 09

Re: To use log4net custom appender, config change only?

Posted 08 June 2009 - 10:52 AM

Yes. That's straightforward in the documentation. I finally solved the problem. I compiled my app with the source-compiled debug version of log4net and stepped through their code. The XmlConfigurator calls ParseAppender for each appender it finds in the config file. That, in turn, calls Utils.SystemInfo.GetTypeFromString() which was exceptioning out in the else clause on Type.GetType(). It was mismatching on the Assembly with 'PUBLIC KEY TOKEN'. Took me a little while (Forest/Trees thing).
Clarification:
When you specify the assembly in the config file, the format is 'class, assembly'. Again, when I had my appender dll added as a reference in my app's Visual Studio project, there was no issue - just put the assembly name, e.g., my assembly is FooAppender.dll, with a class BarCo.FooLogger so the element in my config would be 'BarCo.FooLogger, FooAppender'
Solution:
I needed to put as much of the standard assembly specifiers as needed. So what I ended up was:
'BarCo.FooLogger, FooAppender Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'

Of course, fill in the attributes to match your actual assembly, i.e., if you have a different token or culture.

What a pain. Once again, a little bit of clarification on the documentation would go a long way in saving people time.

View Postmasteryee, on 8 Jun, 2009 - 08:04 AM, said:

Have you tried using the appender-ref tag?

<log4net>
    <appender name="Appender1" />
    <appender name="Appender2" />
    <root>
        <appender-ref ref="Appender1" />
        <appender-ref ref="Appender2"/>
    </root>
</log4net>


Was This Post Helpful? 0
  • +
  • -

#6 tjwin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-June 09

Re: To use log4net custom appender, config change only?

Posted 08 June 2009 - 10:57 AM

Btw - before anyone says 'RTM' - or even the comment above '...how .NET finds assemblies is beyond the scope of this FAQ' - I say emphatically WRONG!
It's not beyond the scope - just one line of comment/example such as:
(NOTE: The Assembly attribute can be solely the name of the DLL or a partially or fully qualified specification such as 'MyLoggerNameSpace.MyLoggerClass, MyAppender Version=1.0.0.0, Culture=neutral, PublicKeyToken=9078234009823409')

Nice, simple, there.
Was This Post Helpful? 0
  • +
  • -

#7 korn_bo2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-June 09

Re: To use log4net custom appender, config change only?

Posted 21 June 2009 - 08:59 PM

View Posttjwin, on 8 Jun, 2009 - 09:57 AM, said:

Btw - before anyone says 'RTM' - or even the comment above '...how .NET finds assemblies is beyond the scope of this FAQ' - I say emphatically WRONG!
It's not beyond the scope - just one line of comment/example such as:
(NOTE: The Assembly attribute can be solely the name of the DLL or a partially or fully qualified specification such as 'MyLoggerNameSpace.MyLoggerClass, MyAppender Version=1.0.0.0, Culture=neutral, PublicKeyToken=9078234009823409')

Nice, simple, there.


hi.. I'am having an error referencing a external dll.. I created a class inherited from AppenderSkeleton.. then compiled as dll...

In my test application, I specify the dll as class, assembly. But it says I have an error finding the Appender.

Do we need to create signing certificates and make it a strong name assembly? anything I am missing here?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1