1.在VS的管理Nuget中安装 log4net
2.配置 webconfig
在configurations 节点 插入如下代码:
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
然后再配置完整的log4net信息:我将error与info信息分开记录
<log4net> <!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL --> <!-- 设置日志类型,root节点比logger优先 --> <!-- <root> <level value="ALL" /> <appender-ref ref="info_log" /> </root> --> <!-- 设置节点 --> <logger name="InfoLogger"> <level value="INFO" /> <appender-ref ref="info_log" /> </logger> <logger name="ErrorLogger"> <level value="ERROR" /> <level value="WARN" /> <appender-ref ref="error_log" /> </logger> <!-- 节点信息明细设置 ,讲info与error分别记录--> <appender name="info_log" type="log4net.Appender.RollingFileAppender"> <param name="File" value="log4/" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value=""Logs_"yyyyMMdd".txt"" /> <param name="StaticLogFileName" value="false" /> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> </layout> </appender> <appender name="error_log" type="log4net.Appender.RollingFileAppender"> <param name="File" value="log4/" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value=""Error_"yyyyMMdd".txt"" /> <param name="StaticLogFileName" value="false" /> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> </layout> </appender> </log4net>
代码中的使用,使用2个帮助类:
public static class LogFactory { /// <summary> /// 解析Xml的路径 /// </summary> public const string Config = "~/Web.Config"; /// <summary> /// 错误日志的标签 /// </summary> public const string Error = "ErrorLogger"; /// <summary> /// 日志记录的标签 /// </summary> public const string Info = "InfoLogger"; /// <summary> /// 初始化 /// </summary> public static void LogInitialize() { log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(System.Web.HttpContext.Current.Server.MapPath(Config))); } }
loghelper.cs
/// <summary> 日志辅助类 log4net /// </summary> public static class LogHelper { /// <summary> 错误日志 /// </summary> /// <param name="msg"></param> public static void Error(string msg) { var log = LogManager.GetLogger(LogFactory.Error); log.Error(msg); } /// <summary> 错误日志,带异常 /// </summary> /// <param name="msg"></param> /// <param name="ex"></param> public static void Error(string msg, Exception ex) { var log = LogManager.GetLogger(LogFactory.Error); log.Error(msg, ex); } /// <summary> 日志记录 /// </summary> /// <param name="msg"></param> public static void Info(string msg) { var log = LogManager.GetLogger(LogFactory.Info); log.Info(msg); } /// <summary> 日志记录 /// </summary> /// <param name="msg"></param> /// <param name="ex"></param> public static void Info(string msg, Exception ex) { var log = LogManager.GetLogger(LogFactory.Info); log.Info(msg, ex); } /// <summary> Debug日志记录 /// </summary> /// <param name="msg"></param> public static void Debug(string msg) { var log = LogManager.GetLogger(LogFactory.Info); log.Debug(msg); } /// <summary> Debug日志记录 /// </summary> /// <param name="msg"></param> /// <param name="ex"></param> public static void Debug(string msg, Exception ex) { var log = LogManager.GetLogger(LogFactory.Info); log.Debug(msg, ex); } /// <summary> Format日志记录 /// </summary> /// <param name="format"></param> /// <param name="arg0"></param> public static void Format(string format, object arg0) { var log = LogManager.GetLogger(LogFactory.Info); log.InfoFormat(format, arg0); } /// <summary> Format日志记录 /// </summary> /// <param name="format"></param> /// <param name="arg0"></param> /// <param name="arg1"></param> public static void Format(string format, object arg0, object arg1) { var log = LogManager.GetLogger(LogFactory.Info); log.InfoFormat(format, arg0, arg1); } /// <summary> Format日志记录 /// </summary> /// <param name="format"></param> /// <param name="arg0"></param> /// <param name="arg1"></param> /// <param name="arg2"></param> public static void Format(string format, object arg0, object arg1, object arg2) { var log = LogManager.GetLogger(LogFactory.Info); log.InfoFormat(format, arg0, arg1, arg2); } /// <summary> Format日志记录 /// </summary> /// <param name="format"></param> /// <param name="provider"></param> /// <param name="args"></param> public static void Format(IFormatProvider provider, string format, params object[] args) { var log = LogManager.GetLogger(LogFactory.Info); log.InfoFormat(provider, format, args); } }
在Global.asax中配置初始化
LogFactory.LogInitialize();
测试页面调用:
public ActionResult Login(LoginModel loginM) { //返回结果类 ResultModel rd = new ResultModel(); //接收前端数据 string _account = loginM.Account; string _password = loginM.password; //调用api验证获取返回值 LogHelper.Info("调用api记录"); //记录日志 //登陆成功,授权并记录 rd.Status = statuslist.成功; rd.ResultMessage = "数据已经收到"; rd.ResulData = new string[] { _account, _password };//数组 //赋予jsonresult data rd.Data = new { rd.Status, rd.ResultMessage, rd.ResulData }; return rd; }
保存日志文件内容为乱码的情况,需要在log4net设置中添加:
<param name="Encoding" value="utf-8" />
即可。