引用log4net
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/// <summary>
/// UiLogEventArgs.cs
/// </summary>
namespace MainAppGree.Log
{
class UiLogEventArgs : EventArgs
{
public string Message { get; private set; }
public UiLogEventArgs(string message)
{
Message = message;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net.Appender;
using log4net.Core;
/// <summary>
/// UiLogAppender.cs
/// </summary>
namespace MainAppGree.Log
{
class UiLogAppender : AppenderSkeleton
{
public event EventHandler<UiLogEventArgs> 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);
}
}
}
}
具体调用方法:
页面中有一个TextBox控件txtLog
#region 全局变量
public static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#endregion
private void DlgRVTaskRun_Load(object sender, EventArgs e)
{
//日志
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 += LogEventHandle;
}
}
private void LogEventHandle(object o, UiLogEventArgs e)
{
// 解决线程占用问题
if (txtLog.InvokeRequired)
{
txtLog.Invoke(new System.Action(() => { txtLog.AppendText(e.Message + "\r\n"); }));
}
else
{
txtLog.AppendText(e.Message + "\r\n");
}
}
Log.Info();
Log.Debug();
Log.Error();
MainApp.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<!--日志记录组建配置-->
<log4net>
<!-- Console部分log输出格式的设定 -->
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger %ndc - %message%newline" />
</layout>
</appender>
<!-- UI日志 -->
<appender name="uiLogAppender" type="MainAppGree.Log.UiLogAppender,MainAppGree">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{HH:mm:ss,fff} %-5level - %message" />
</layout>
<threshold value="INFO" />
</appender>
<!-- 日志文件部分log输出格式的设定 -->
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="D:\MainApp\Logs\MainAppLog_" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd'.log'" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.DynamicPatternLayout">
<param name="Header" value="%newline**** Trace Opened Local: %date{yyyy-MM-dd HH:mm:ss.fff} UTC: %utcdate{yyyy-MM-dd HH:mm:ss.fff} ****%newline"/>
<param name="Footer" value="%newline#### Trace Closed Local: %date{yyyy-MM-dd HH:mm:ss.fff} UTC: %utcdate{yyyy-MM-dd HH:mm:ss.fff} ####%newline"/>
<ConversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<root>
<level value="ALL" />
<appender-ref ref="uiLogAppender" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
</configuration>
————————————————
版权声明:本文为CSDN博主「Carpe_D1em」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_35106907/article/details/86211677
来源:CSDN
作者:qq_18932003
链接:https://blog.csdn.net/qq_18932003/article/details/104076707