简体   繁体   中英

Log4Net: Custom appender from referenced DLL is ignored

I created a very simple appender from subclassing AppenderSkeleton . I put this into an extra library and referenced it. The relevant part of my app.config in the program where I use it is:

  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>
  <log4net>
    <appender name="FileAppender" type="log4net.Appender.FileAppender">
      <file value="${LOCALAPPDATA}\MyProgram\MyLog.txt" />
      <appendToFile value="true" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger - %message%newline%exception" />
      </layout>
      <threshold value="Debug" />
    </appender>
    <appender name="MySimpleAppender" type="Some.Namespace.SimpleAppender" >
      <threshold value="Info" />
    </appender>
    <root>
      <appender-ref ref="FileAppender" />
      <appender-ref ref="MySimpleAppender" />
    </root>
  </log4net>

The configurator is setup in Program.cs like this:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

When I now log something, eg:

LOG.Info("foo");

the message gets written to the file (using the FileAppender ), but my SimpleAppender is ignored. When I explicitly create a nullary constructor and set a breakpoint to it, I see that it is not even instantiated. When I copy the class file directly into the project where my program resides in, it works.

What am I missing?

You need to add the assembly name to the type tag. Right now it is looking in the Log4Net assembly.

It takes the format

type="Namespace,AssemblyName"

So much like

 <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>

You need

<appender name="MySimpleAppender" type="Some.Namespace.SimpleAppender,MyAssembly" >

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM