引用log4net
点击项目的Nuget管理。搜索log4net 选择最新版本点击安装。
安装完毕后选择项目引用项的引用查看:
配置log4net 参数设置点击app.config 加入
<?xml version="1.0" encoding="utf-8" ?><configuration> <configSections> <ction name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> </configSections> <log4net> <root> </root> <logger name="Error"> <level value="ALL" /> <appender-ref ref="ErrorAppender" /> </logger> <logger name="InfoLog"> <level value="ALL" /> <appender-ref ref="InfoAppender" /> </logger> <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender,log4net" > <param name="File" type="" value="Log/Error/" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value="yyyyMMdd" /> <param name="StaticLogFileName" value="fal" /> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%n[%d] %c %n%m%n%n" /> <param name="Header" value="-----------BEGIN-----------" /> <param name="Footer" value="------------END------------" /> </layout> </appender> <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender,log4net" > <param name="File" type="" value="Log/Info/" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value="yyyyMMdd" /> <param name="StaticLogFileName" value="fal" /> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%n[%d] %c %n%m%n%n" /> <param name="Header" value="-----------BEGIN-----------" /> <param name="Footer" value="------------END------------" /> </layout> </appender> </log4net></configuration>
引入使用lognet生效
[STAThread] static void Main() { log4net.Config.XmlConfigurator.Configure(); // To customize application configuration such as t high DPI ttings or default font, // e https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); Application.Run(new Frm.FrmCloudLed()); }
创建调用公共类
public class Log4netHelper { /// <summary> /// 普通日志 /// </summary> /// <param name="message">日志内容</param> public static void Info(string message) { log4net.ILog log = log4net.LogManager.GetLogger("InfoLog"); if (log.IsInfoEnabled) { log.Info(message); } log = null; } /// <summary> /// 错误日志 author huochengyan /// </summary> /// <param name="message">错误日志</param> public static void Error(string message) { log4net.ILog log = log4net.LogManager.GetLogger("Error"); if (log.IsInfoEnabled) { log.Error(message); } log = null; } // log4netHelper.Info("这是一条普通日志"); //log4netHelper.Error("这是一条错误日志"); }
调用
// log4netHelper.Info("这是一条普通日志"); //log4netHelper.Error("这是一条错误日志");
查看日志效果:
本文发布于:2023-02-28 20:58:00,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/zhishi/a/167771177698887.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:log4net(Log4Net应用).doc
本文 PDF 下载地址:log4net(Log4Net应用).pdf
留言与评论(共有 0 条评论) |