一、添加引用
二、添加Log4Net配置文件,设置文件属性 如果较新则复制或者始终复制
<?xml version="1.0" encoding="utf-8"?> <log4net> <!-- 将日志以回滚文件的形式写到文件中 --> <!-- 按日期切分日志文件,并将日期作为日志文件的名字 --> <appender name="Log4Name" type="log4net.Appender.RollingFileAppender"> <!--不加utf-8编码格式,中文字符将显示成乱码--> <param name="Encoding" value="utf-8" /> <file value="Log/"/> <appendToFile value="true" /> <rollingStyle value="Date" /> <!--日期的格式,每天换一个文件记录,如不设置则永远只记录一天的日志,需设置--> <datePattern value="yyyy-MM-dd'.txt'"/> <!--日志文件名是否为静态--> <StaticLogFileName value="false"/> <!--多线程时采用最小锁定--> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <!--布局(向用户显示最后经过格式化的输出信息)--> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%-5level %date{HH:mm:ss} %message%n"/> </layout> </appender> <root> <!-- 控制级别,由低到高:ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF --> <!-- 比如定义级别为INFO,则INFO级别向下的级别,比如DEBUG日志将不会被记录 --> <!-- 如果没有定义LEVEL的值,则缺省为DEBUG --> <level value="ALL" /> <!-- 按日期切分日志文件,并将日期作为日志文件的名字 --> <appender-ref ref="Log4Name"/> </root> </log4net>
三、加载log4net配置
方法一:在AssemblyInfo类中指定log4net的配置文件
方法二:在代码中添加
四、使用方法
/// <summary> /// log4net日志专用 /// </summary> public class LogHelper { private static readonly ILog Instance = LogManager.GetLogger("LogHelper"); public static void SetConfig() { XmlConfigurator.Configure(); } public static void SetConfig(string filePath) { FileInfo configFile = new FileInfo(filePath); XmlConfigurator.Configure(configFile); } public static void SetConfig(FileInfo configFile) { XmlConfigurator.Configure(configFile); } /// <summary> /// 记录普通文件记录 /// </summary> /// <param name="info"></param> public static void Info(string info) { if (Instance.IsInfoEnabled) { Instance.Info(info); } } /// <summary> ///记录调试信息 /// </summary> /// <param name="info"></param> public static void Debug(string info) { if (Instance.IsErrorEnabled) { Instance.Debug(info); } } /// <summary> ///记录警告信息 /// </summary> /// <param name="info"></param> public static void Warn(string info) { if (Instance.IsWarnEnabled) { Instance.Warn(info); } } /// <summary> /// 记录错误日志 /// </summary> /// <param name="info"></param> /// <param name="se"></param> public static void Error(string info, Exception se) { if (Instance.IsErrorEnabled) { Instance.Error(info, se); } } /// <summary> /// 记录严重错误 /// </summary> /// <param name="info"></param> /// <param name="se"></param> public static void Fatal(string info, Exception se) { if (Instance.IsFatalEnabled) { Instance.Fatal(info, se); } } }
五、显示日志到界面
1、新建 LogAppender 类继承AppenderSkeleton
public class LogAppender : AppenderSkeleton { public event Action<object> LogAppendEvent; protected override void Append(LoggingEvent loggingEvent) { string log; if (this.Layout != null) { PatternLayout patternLayout = this.Layout as PatternLayout; log = patternLayout.Format(loggingEvent); if (loggingEvent.ExceptionObject != null) { log += loggingEvent.ExceptionObject.ToString(); } } else { log = loggingEvent.RenderedMessage; } LogAppendEvent?.Invoke(log); } }
2、使用LogAppender 类
public Dispatcher Dispatcher { get; set; } = Dispatcher.CurrentDispatcher; public ObservableCollection<string> LogMsgs { get; set; } = new ObservableCollection<string>();//日志框的日志集合 private void InitLog() { //加载日志配置文件,只需要在程序启动时加载一次 //string filePath = AppDomain.CurrentDomain.BaseDirectory + "Log4net.config"; //LogHelper.SetConfig(filePath); string logPattern = "%-5p %d{HH:mm:ss} %m"; LogAppender logAppender = new LogAppender() { Layout = new PatternLayout(logPattern) }; //logAppender.Name = "LogAppenderUI"; logAppender.LogAppendEvent += ShowLog; //IAppender[] appenders = new IAppender[] //{ // logAppender //}; BasicConfigurator.Configure(logAppender); } private void ShowLog(object log) { try { if (LogMsgs.Count > 1500) { LogMsgs.Clear(); } Dispatcher.Invoke(() => { LogMsgs.Insert(0, log.ToString()); }); } catch (Exception e) { LogHelper.Debug(e.Message); } string message = log.ToString(); }
xaml代码
标签:Log4Net,info,string,void,public,Instance,static,日志,WPF From: https://www.cnblogs.com/othersheartBlog/p/17932335.html