1

大家好,我正在尝试扩展AsyncForwardingAppender。我有以下配置文件:

<log4net>
    <appender name="AsyncForwardingAppenderExtender" type="log4net.Async.AsyncForwardingAppender,Log4net.Async" >
    <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
    </layout>
    </appender>
    <root>
        <level value="Info" />
        <appender-ref ref="AsyncForwardingAppenderExtender" />
    </root>
</log4net>

现在在我的代码中,我已经覆盖了Append如下方法

public class AsyncForwardingAppenderExtender : Log4Net.Async.AsyncForwardingAppender
{
    private Queue<LoggingEvent> buffer = new Queue<LoggingEvent>();
    private static readonly Type ThisType = typeof (AsyncForwardingAppender);

    public AsyncForwardingAppenderExtender()
    {
    }

    protected override void Append(LoggingEvent loggingEvent)
    {
        buffer.Enqueue(loggingEvent);
    }

    protected override void Append(LoggingEvent[] loggingEvents)
    {
        foreach (var loggingEvent in loggingEvents)
        {
            Append(loggingEvent);
        }
    }

    public override void ActivateOptions()
    {
        base.ActivateOptions();
        InitializeBuffer();
        StartForwarding();
    }

    private void StartForwarding()
    {
        Thread forwardingThread =
            new Thread(ForwardingThreadExecute)
            {
                Name = String.Format("{0} Forwarding Appender Thread", Name),
                IsBackground = false,
            };
        forwardingThread.Start();
    }

    public void InitializeBuffer()
    {
        buffer = new Queue<LoggingEvent>();
    }

    private void ForwardingThreadExecute()
    {
        try
        {
            ForwardLoggingEventsFromBuffer();
        }
        catch (Exception exception)
        {
            // ForwardInternalError("Unexpected error in asynchronous forwarding loop", exception, ThisType);
        }
    }

    private void ForwardLoggingEventsFromBuffer()
    {
        CLSAdapter adapter = new CLSAdapter();
        while (buffer.Count > 0)
        {
            LoggingEvent l = buffer.Dequeue();
            LoggingEventData data = l.GetLoggingEventData();
            adapter.CreateConnection(data.Message);
        }
    }
}

Append正在追加到队列中。然后StartForwading通过一个函数从队列推送到 Splunk。

问题是:Append当我Log.Fatal("hhhh")从我的程序中执行类似操作时,没有被触发。创建此自定义附加程序时我缺少什么?

4

1 回答 1

0
<appender name="AsyncForwardingAppenderExtender" type="log4net.Async.AsyncForwardingAppender,Log4net.Async" >

类型错误 - 应该是您的 AsyncForwardingAppenderExtender

于 2017-06-29T16:22:06.637 回答