Search code examples
c#log4netnlogappender

Custom NLog Appender


I trying to write custom appender for logging in NLog. I saw some examlpes for log4net where should write appender which is inherit from abstract class AppenderSkeleton. Can anyone name the analog class in NLog?


Solution

  • NLog analog of log4net's appenders will be target. For creating your own target, you have to inherit from NLog.Targets.TargetWithLayout. Also you should mark your target class with attribute TargetAttribute:

    [Target("Foo")]
    public class FooTarget : TargetWithLayout
    {
        protected override void Write(LogEventInfo logEvent)
        {            
            Console.WriteLine(logEvent.Message);
        }
    }
    

    Next step is adding assembly where your class is defined to NLog extensions:

    <nlog>
      <extensions>
        <add assembly="MyBarAssembly"/>
      </extensions>
      <targets>
         ...
    

    And last step - registering your target (NLog will search in extensions for types market by TargetAttribute)

    <target name="foo" type="Foo"/>