1.下载log4net
2编写log4net的配置文件
3编写帮助类
using log4net;using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Reflection;using System.Text;using System.Text.RegularExpressions;using System.Threading.Tasks;using System.Web;namespace TestData.Common{ public static class HadleExcption { //判断是否是绝对路径 public static bool IsPhysicalPath(string Path) { string RegexString = @"\b[a-z]:\\.*"; MatchCollection Matchs = Regex.Matches(Path, RegexString, RegexOptions.IgnoreCase | RegexOptions.Compiled); if (Matchs.Count > 0) return true; return false; } public static void WriteLog(this Exception ex, string message) { string _path = ""; if (!IsPhysicalPath("~/Config/lognet.config")) _path = HttpContext.Current.Server.MapPath("~/Config"); else _path = "~/Config/lognet.config"; FileInfo info = null; if (new DirectoryInfo(_path).Exists) { info = new FileInfo(_path + "/lognet.config"); } log4net.Config.XmlConfigurator.ConfigureAndWatch(info); log4net.ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); log.Info(message, ex); } }}