When we asp will deploy to the remote server, if the program encounters an error, how to quickly determine the error of our program yet. So ->
Log4Net logging as a major artifact, and had to learn to skillfully use ah!
Not so much the principle of using Zhaomaohuahu to ensure that you can successfully use simple logging tool.
In WinForm
the five steps to good useLog4Net
//第一步:Nuget安装log4net --> Install-Package log4net
//第二步:在项目文件根目录加入文件log4net.config(如下,直接拷贝使用)
<!-- Level的级别,由高到低 -->
<!-- None > Fatal > ERROR > WARN > DEBUG > INFO > ALL-->
<!-- 解释:如果level是ERROR,则在cs文件里面调用log4net的info()方法,则不会写入到日志文件中-->
<log4net> <!--错误日志类--> <logger name="logerror"><!--日志类的名字--> <level value="ALL" /><!--定义记录的日志级别--> <appender-ref ref="ErrorAppender" /><!--记录到哪个介质中去--> </logger> <!--信息日志类--> <logger name="loginfo"> <level value="ALL" /> <appender-ref ref="InfoAppender" /> </logger> <!--错误日志附加介质--> <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender"><!-- name属性指定其名称,type则是log4net.Appender命名空间的一个类的名称,意思是,指定使用哪种介质--> <param name="File" value="Log\\LogError\\" /><!--日志输出到exe程序这个相对目录下--> <param name="AppendToFile" value="true" /><!--输出的日志不会覆盖以前的信息--> <param name="MaxSizeRollBackups" value="100" /><!--备份文件的个数--> <param name="MaxFileSize" value="10240" /><!--当个日志文件的最大大小--> <param name="StaticLogFileName" value="false" /><!--是否使用静态文件名--> <param name="DatePattern" value="yyyyMMdd".htm"" /><!--日志文件名--> <param name="RollingStyle" value="Date" /><!--文件创建的方式,这里是以Date方式创建--> <!--错误日志布局--> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="<HR COLOR=red>%n异常时间:%d [%t] <BR>%n异常级别:%-5p <BR>%n异 常 类:%c [%x] <BR>%n%m <BR>%n <HR Size=1>" /> </layout> </appender> <!--信息日志附加介质--> <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="Log\\LogInfo\\" /> <param name="AppendToFile" value="true" /> <param name="MaxFileSize" value="10240" /> <param name="MaxSizeRollBackups" value="100" /> <param name="StaticLogFileName" value="false" /> <param name="DatePattern" value="yyyyMMdd".htm"" /> <param name="RollingStyle" value="Date" /> <!--信息日志布局--> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="<HR COLOR=blue>%n日志时间:%d [%t] <BR>%n日志级别:%-5p <BR>%n日 志 类:%c [%x] <BR>%n%m <BR>%n <HR Size=1>" /> </layout> </appender> </log4net>
Note that, in the new log4net.config file, the property changed as shown in
//第三步:在AssemblyInfo.cs文件中加入这条语句
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)]
//第四步:在项目文件根目录(其实无所谓,想建在哪里都是阔以滴)建一个LogHelper.cs类 public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo"); public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror"); public static void WriteLog(string info) { if (loginfo.IsInfoEnabled) { loginfo.Info(info); } } public static void WriteLog(string info, Exception se) { if (logerror.IsErrorEnabled) { logerror.Error(info, se); } } //第五步:使用 LogHelper.WriteLog(string.Format("当前时间为{0}.", DateTime.Now.ToString()));
After the correct output following views
other:
https://www.cnblogs.com/lsgsanxiao/p/5845300.html