Log4Net MemoryAppender似乎错过了高速日志事件 [英] Log4Net MemoryAppender seems to miss high speed log events

查看:131
本文介绍了Log4Net MemoryAppender似乎错过了高速日志事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用log4net MemoryAppender在表单文本框中显示我的log4net输出.我的配置文件的相关部分是:

I am using a log4net MemoryAppender to show my log4net output on a form text box. The relevant part of my config file is:

<appender name="MemoryAppender" type="log4net.Appender.MemoryAppender" >
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%level %date %message%newline" />
    </layout>
</appender>

更新表单的代码由计时器触发,如下所示:

The code that updates the form is fired by a timer and looks like:

var events = _memoryAppender.GetEvents();

foreach (var loggingEvent in events)
{
    textBoxOutput.Text += loggingEvent.Level + "  " + loggingEvent.MessageObject + Environment.NewLine;
}
_memoryAppender.Clear();

它正在运行,但是当我的应用程序在短时间内记录很多消息时,我似乎看不到它们.有谁知道发生了什么事?

It is working, but when my application logs a lot of messages in a short period of time I don't seem to see them. Does anyone have an idea what is going on?

作为一种解决方法,我正在使用链锯和UDP附加程序:

As a workaround I am using Chainsaw and a UDP appender:

   <appender name="UdpAppender" type="log4net.Appender.UdpAppender">
    <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="INFO"/>
        <param name="LevelMax" value="ERROR"/>
    </filter>
    <param name="RemoteAddress" value="127.0.0.1" />
    <param name="RemotePort" value="8080" />
    <layout type="log4net.Layout.XmlLayoutSchemaLog4j, log4net" />
</appender>

不过,我更喜欢自己的UI,所以我还在寻找答案.

I like my own UI better, though, so I am still looking for answers.

推荐答案

我假设会发生以下情况:在文本框中编写日志消息时,会记录新消息.当您清除附加程序中的消息时,您还将删除这些新消息,这些新消息未写入文本框.

I assume the following happens: While you are writing your log messages in the text box, new messages are logged. When you clear the messages in the appender you also remove these new messages, which are not written to the text box.

您可以通过在开始将消息写入文本框之前清除附加程序来改善这种情况,但是我认为您应该创建自己的内存附加程序,并提供一种返回消息并一步一步清除数组的方法.像这样:

You could improve the situation by clearing the appender before starting to write messages to the text box but I think you should create your own memory appender and provide a method that returns the messages and clears the array in one step. Something like this:

public class MyMemoryAppender : MemoryAppender
{
    public LoggingEvent[] GetAndClearEvents()
    {
        lock (m_eventList.SyncRoot)
        {
            var events = (LoggingEvent[]) m_eventsList.ToArray(typeof(LoggingEvent));
            m_eventsList.Clear();
            return events;
        }
    }
}

这篇关于Log4Net MemoryAppender似乎错过了高速日志事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆