news 2026/4/16 12:14:45

VisionPro二开之日志Log模块

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
VisionPro二开之日志Log模块

VisionPro二开之日志Log模块

一 配置log4net.config文件

<?xmlversion="1.0"encoding="utf-8"?><configuration><configSections><sectionname="log4net"type="System.Configuration.IgnoreSectionHandler"/></configSections><log4net><!--配置日志的目标存储形式--><root><!--输出等级--><levelvalue="ALL"/><!--控制台形式--><appender-refref="ConsoleAppender"/><!--文件形式--><appender-refref="FileAppender"/></root><!--控制台形式--><appendername="ConsoleAppender"type="log4net.Appender.ConsoleAppender"><layouttype="log4net.Layout.PatternLayout"value="%date [%thread] %-5level %logger - %message%newline"/></appender><!--文件形式--><appendername="FileAppender"type="log4net.Appender.FileAppender"><!--文件路径--><filevalue="Log/log-file.txt"/><!--是否追加--><appendToFilevalue="true"/><!--显示格式--><layouttype="log4net.Layout.PatternLayout"><paramname="Header"value="&#xD;&#xA;-------------------------软件启动-----------------------&#xD;&#xA;"/><paramname="Footer"value="&#xD;&#xA;-------------------------软件关闭-----------------------&#xD;&#xA;"/><paramname="ConversionPattern"value="%d{yyyy-MM-dd HH:mm:ss} --%-5p-- %m%n"/></layout></appender></log4net></configuration>

二 绑定log4net.config文件

[assembly:log4net.Config.XmlConfigurator(ConfigFile="log4net.config",ConfigFileExtension="config",Watch=true)]

三 创建文件夹-LogModule

Log

usinglog4net;usinglog4net.Layout;usinglog4net.Repository.Hierarchy;usingMicrosoft.VisualBasic.Logging;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceAOI缺陷检测软件_VisionPro{publicclassLog{// 1.获取log4net里面的日志类实例publicstaticILoglog=LogManager.GetLogger("myLog");// 2.根据类实现5个等级日志的记录publicstaticvoidInfo(stringmsg){log.Info(msg);}publicstaticvoidDebug(stringmsg){log.Debug(msg);}publicstaticvoidWarn(stringmsg){log.Warn(msg);}publicstaticvoidError(stringmsg){log.Error(msg);}publicstaticvoidFatal(stringmsg){log.Fatal(msg);}// 3.把日志内容同步到显示控件上publicstaticvoidInitTextBox(TextBoxtextBox){if(textBox==null)return;varlogPattern="%d{yyyy-MM-dd HH:mm:ss:fff} --%-5p-- %m%n";TextBoxBaseAppenderappender=newTextBoxBaseAppender();appender.TextBox=textBox;// 核心appender.Layout=newPatternLayout(logPattern);Loggerlog4netLogger=log.LoggerasLogger;log4netLogger.AddAppender(appender);}}}

TextBoxBaseAppender

usinglog4net.Appender;usinglog4net.Core;usinglog4net.Layout;usingSystem;usingSystem.Collections.Generic;usingSystem.Diagnostics;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceAOI缺陷检测软件_VisionPro{/// <summary>////// </summary>/// <summary>/// 文本框添加信息类/// </summary>publicclassTextBoxBaseAppender:AppenderSkeleton{/// <summary>/// 文本输入框/// </summary>publicTextBoxTextBox{get;set;}/// <summary>/// 构造器/// </summary>staticTextBoxBaseAppender(){}/// <summary>/// 复现Append方法,记录日志,就会调用这个方法/// </summary>/// <param name="loggingEvent"></param>protectedoverridevoidAppend(LoggingEventloggingEvent){PatternLayoutpatternLayout=(PatternLayout)this.Layout;stringstr=string.Empty;if(patternLayout!=null){str=patternLayout.Format(loggingEvent);if(loggingEvent.ExceptionObject!=null)str+=loggingEvent.ExceptionObject.ToString()+Environment.NewLine;}elsestr=loggingEvent.LoggerName+"-"+loggingEvent.RenderedMessage+Environment.NewLine;// 打印printf(str);}privateboolm_Flag=false;//线程开启privateList<string>m_LogStrList=newList<string>();/// <summary>/// 锁/// </summary>privateObjectm_LockObj=newObject();/// <summary>/// 打印信息/// </summary>/// <param name="str"></param>privatevoidprintf(stringstr){lock(m_LockObj){Debug.WriteLine(str);m_LogStrList.Add(str);}if(m_Flag==false){m_Flag=true;Task.Run(()=>{while(true){Thread.Sleep(300);//日志刷新周期300ms 避免抢占主线程if(m_LogStrList.Count==0)continue;List<string>tempList=newList<string>();lock(m_LockObj){for(inti=0;i<m_LogStrList.Count;i++){tempList.Add(m_LogStrList[i]);}m_LogStrList.Clear();}if(tempList.Count>0){try{// 判别控件是否创建if(TextBox.IsHandleCreated){// 跨线程TextBox.Invoke(newAction(()=>{try{if(TextBox.Lines.Length>100){TextBox.Clear();}foreach(varstrTempintempList){ListViewItemitem=newListViewItem();item.Text=strTemp.ToString();TextBox.AppendText(strTemp);}}catch(Exceptione){throwe;}}));}}catch(TaskCanceledException){//TaskCanceledException 异常是关闭时候可能出现 直接忽略}catch(Exception){throw;}}}});}}}}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 10:39:12

AI侦测模型数据标注:云端协作工具+GPU加速全攻略

AI侦测模型数据标注&#xff1a;云端协作工具GPU加速全攻略 引言 当你需要处理10万张图片的数据标注任务时&#xff0c;是否遇到过这些困扰&#xff1f;本地电脑跑不动、团队成员协作困难、标注进度缓慢...这些问题我都经历过。今天我要分享的云端协作标注方案&#xff0c;正…

作者头像 李华
网站建设 2026/4/8 10:40:31

AI智能侦测开箱即用方案: Docker镜像+示例代码全家桶

AI智能侦测开箱即用方案&#xff1a; Docker镜像示例代码全家桶 引言&#xff1a;为什么你需要这个方案&#xff1f; 想象一下&#xff0c;你是一个外包团队的负责人&#xff0c;刚接到一个AI项目&#xff0c;客户催着要demo&#xff0c;但团队里没人有AI开发经验。这时候&am…

作者头像 李华
网站建设 2026/4/16 10:16:24

物联网安全AI检测:云端方案1小时部署,守护智能设备

物联网安全AI检测&#xff1a;云端方案1小时部署&#xff0c;守护智能设备 引言&#xff1a;为什么智能家居需要AI安全防护&#xff1f; 早上7点&#xff0c;你的智能闹钟准时响起&#xff0c;窗帘自动拉开&#xff0c;咖啡机开始工作——这是智能家居带来的便利生活。但你是…

作者头像 李华
网站建设 2026/4/16 10:14:01

智能监控方案低成本验证:1小时1块,立即体验AI效能

智能监控方案低成本验证&#xff1a;1小时1块&#xff0c;立即体验AI效能 1. 为什么物业经理需要AI监控方案 作为物业经理&#xff0c;你可能经常面临这样的困扰&#xff1a;小区安保需要24小时值守&#xff0c;但人力成本越来越高&#xff1b;传统监控只能录像不能预警&…

作者头像 李华
网站建设 2026/4/16 10:14:02

实体行为分析3步入门:免配置云端环境,学生党也能轻松玩

实体行为分析3步入门&#xff1a;免配置云端环境&#xff0c;学生党也能轻松玩 引言&#xff1a;当心理学遇上AI行为分析 作为一名心理学研究生&#xff0c;你是否遇到过这样的困境&#xff1a;实验室的老旧电脑连OpenPose都跑不动&#xff0c;而申请经费购买新设备又要等到下…

作者头像 李华