本文共 2071 字,大约阅读时间需要 6 分钟。
1.通过管理NuGet程序包 引入log4net.dll程序集
2.配置log4netConfig.xml文件3.新建公共方法类Log4NetHelper
public class Log4NetHelper { private static readonly ILog Loginfo = LogManager.GetLogger("InfoLog"); private static readonly ILog Logwarn = LogManager.GetLogger("Warn"); private static readonly ILog Logerror = LogManager.GetLogger("ErrorLog"); private static readonly ILog Logfatal = LogManager.GetLogger("Fatal"); static Log4NetHelper() { var configFile = HostingEnvironment.MapPath("/App_Data/Log4netConfig.xml"); log4net.Config.XmlConfigurator.Configure(new FileInfo(configFile)); //log4net.Config.XmlConfigurator.Configure(); } private static string GetMethodInfo() { StackTrace ss = new StackTrace(true); //index:0为本身的方法;1为调用方法;2为其上上层,依次类推 MethodBase mb = ss.GetFrame(2).GetMethod(); string systemModule = $"{Environment.NewLine}模块名:{mb.Module}{Environment.NewLine}命名空间名:{mb.DeclaringType?.Namespace}{Environment.NewLine}类名:{mb.DeclaringType?.Name}{Environment.NewLine}方法名:{mb.Name}"; return systemModule; } public static void Info(string msg) { Loginfo.Info($"{GetMethodInfo()}——{msg}"); } public static void Error(string msg) { Logerror.Error($"{GetMethodInfo()}——{msg}"); } } }
转载地址:http://zacjn.baihongyu.com/