当我们将asp程序部署到远程服务器上的时候,如果遇到程序错误,如何能快速的判断我们程序的错误呢。所以--> Log4Net作为记录日志的一大神器,不得不学会熟练使用啊! 没有那么多的原理,照猫画虎的使用,保证你也能成功的使用这么简单的日志记录工具。
在WinForm
中做好五步即可使用Log4Net
//第一步: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>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- Level的级别,由高到低 -->
<!-- None > Fatal > ERROR > WARN > DEBUG > INFO > ALL-->
<!-- 解释:如果level是ERROR,则在cs文件里面调用log4net的info()方法,则不会写入到日志文件中-->
<log4net>
<logger name="SysRFLogger">
<level value="DEBUG" />
<appender-ref ref="SysRFAppender" />
</logger>
<logger name="DebugRFLogger">
<level value="DEBUG" />
<appender-ref ref="DebugAppender" />
</logger>
<logger name="MsgLogger">
<level value="DEBUG" />
<appender-ref ref="MsgAppender" />
</logger>
<logger name="OperInfoLogger">
<level value="DEBUG" />
<appender-ref ref="OperInfoAppender" />
</logger>
<appender name="SysRFAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs\syslog.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<preserveLogFileNameExtension value="true" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="DebugAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs\sysDebuglog.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<preserveLogFileNameExtension value="true" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="MsgAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs\Messagelog.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<preserveLogFileNameExtension value="true" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="OperInfoAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=4.0.30319.18020, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<commandText value="INSERT INTO LOG_USEROPERLOG ([ID],[Date],[User],[Flag],[OperInfo],[OperFlag],[OperResult],[Message]) VALUES (@Id,@log_date, @user, @flag, @operinfo,@operflag, @operresult, @message)" />
<parameter>
<parameterName value="@Id" />
<dbType value="String" />
<size value="36" />
<layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log">
<param name="ConversionPattern" value="%property{ID}" />
</layout>
</parameter>
<parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@user" />
<dbType value="String" />
<size value="64" />
<layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log">
<param name="ConversionPattern" value="%property{User}" />
</layout>
</parameter>
<parameter>
<parameterName value="@flag" />
<dbType value="Int32" />
<layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log">
<param name="ConversionPattern" value="%property{Flag}" />
</layout>
</parameter>
<parameter>
<parameterName value="@operinfo" />
<dbType value="String" />
<size value="255" />
<layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log">
<param name="ConversionPattern" value="%property{OperInfo}" />
</layout>
</parameter>
<parameter>
<parameterName value="@operflag" />
<dbType value="String" />
<size value="32" />
<layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log">
<param name="ConversionPattern" value="%property{OperFlag}" />
</layout>
</parameter>
<parameter>
<parameterName value="@operresult" />
<dbType value="String" />
<size value="255" />
<layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log">
<param name="ConversionPattern" value="%property{OperResult}" />
</layout>
</parameter>
<parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="4000" />
<layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log">
<param name="ConversionPattern" value="%property{Message}" />
</layout>
</parameter>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="SysRFAppender" />
</root>
</log4net>
</configuration>
//第三步:在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()));
我的实例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log4net;
namespace WindowsFormsApp4
{
public static class log4net
{
public static readonly ILog loginfo = LogManager.GetLogger("loginfo");
public static readonly ILog logerror = 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);
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
int a = 0, b = 0;
a = a / b;
}
catch(Exception ex)
{
log4net.WriteLog(ex.ToString());
}
toolStripStatusLabel1.Text = "数据处理完成!!!";
}
正确后输出以下视图
参考链接:
https://www.cnblogs.com/xuxuzhaozhao/p/6640623.html
https://www.jb51.net/article/128865.htm