一些关于怎样把log4net信息输出到UI界面的
答案:1 悬赏:0 手机版
解决时间 2021-11-30 16:51
- 提问者网友:書生途
- 2021-11-30 07:20
一些关于怎样把log4net信息输出到UI界面的
最佳答案
- 五星知识达人网友:三千妖杀
- 2021-11-30 07:29
首先定义一个EventArgs
public class UiLogEventArgs : EventArgs
{
public string Message { get; private set; }
public UiLogEventArgs(string message)
{
Message = message;
}
}
然后自定义一个Appender
public class UiLogAppender : AppenderSkeleton
{
public event EventHandler UiLogReceived;
protected override void Append(LoggingEvent loggingEvent)
{
var message = RenderLoggingEvent(loggingEvent);
OnUiLogReceived(new UiLogEventArgs(message));
}
protected virtual void OnUiLogReceived(UiLogEventArgs e)
{
if (UiLogReceived != null)
UiLogReceived(this, e);
}
}
是在XML实现配置log4net的
接下里就是在code中把被待处理的事件注册到UiLogAppender的事件EventHandler中
(首先我们得找到uiLogAppender,然后才把处理UI的方法注册上去)
var hierarchy = LogManager.GetRepository() as Hierarchy;
var appenders = hierarchy.Root.Repository.GetAppenders();
foreach (var appender in appenders)
{
var uiLogAppender = appender as UiLogAppender;
if (uiLogAppender != null)
uiLogAppender.UiLogReceived += ShowMessageOnUi;
}
以上就是把log4net信息同步输出到UI的方法。
public class UiLogEventArgs : EventArgs
{
public string Message { get; private set; }
public UiLogEventArgs(string message)
{
Message = message;
}
}
然后自定义一个Appender
public class UiLogAppender : AppenderSkeleton
{
public event EventHandler UiLogReceived;
protected override void Append(LoggingEvent loggingEvent)
{
var message = RenderLoggingEvent(loggingEvent);
OnUiLogReceived(new UiLogEventArgs(message));
}
protected virtual void OnUiLogReceived(UiLogEventArgs e)
{
if (UiLogReceived != null)
UiLogReceived(this, e);
}
}
是在XML实现配置log4net的
接下里就是在code中把被待处理的事件注册到UiLogAppender的事件EventHandler中
(首先我们得找到uiLogAppender,然后才把处理UI的方法注册上去)
var hierarchy = LogManager.GetRepository() as Hierarchy;
var appenders = hierarchy.Root.Repository.GetAppenders();
foreach (var appender in appenders)
{
var uiLogAppender = appender as UiLogAppender;
if (uiLogAppender != null)
uiLogAppender.UiLogReceived += ShowMessageOnUi;
}
以上就是把log4net信息同步输出到UI的方法。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯