如何从Custom appender(Log4net)获取Layout属性?

时间:2016-08-09 15:32:47

标签: c# log4net pattern-layout

我使用Log4net实现了一个CustomAppender。如何从Appender中的patternLayout访问Layout属性。

  public class CutomAppender : AppenderSkeleton
{
     protected override void Append(LoggingEvent loggingEvent)
    {
      //My custom code here...........
    }
}

配置文件:

<log4net>
 <appender name="CutomAppender" type="log4netCustomAppender.CutomAppender,log4netCustomAppender">   
  <layout type="log4net.Layout.PatternLayout">
   <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
  </layout>
 </appender>
 <root>                                                                               
<level value="ALL" />
<appender-ref ref="CutomAppender" />
 </root>
</log4net>

1 个答案:

答案 0 :(得分:0)

要获取布局信息,只需使用基础AppenderSkeleton类的Layout属性。要获取已在此布局中呈现的消息,请使用此相同基类中的方法RenderLoggingEvent:

public class CutomAppender : AppenderSkeleton
{
    protected override void Append(LoggingEvent loggingEvent)
    {
        string renderedMessage = base.RenderLoggingEvent(loggingEvent);
        ILayout layout = base.Layout;
        // do something here
    }
}