using log4net; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Web; namespace Central.Control.WebApi.Log4net { /// /// 日志工具类 /// public class Log4NetHelper: ILog4NetHelper { public Log4NetHelper() { log4net.Config.XmlConfigurator.Configure(new FileInfo(HttpContext.Current.Server.MapPath("~/App_Data/Config/log4net.config"))); } public void Debug(object message) { LogManager.GetLogger(GetCurrentMethodFullName()).Debug(message); } public void Debug(object message, Exception ex) { LogManager.GetLogger(GetCurrentMethodFullName()).Debug(message, ex); } public void Error(object message) { LogManager.GetLogger(GetCurrentMethodFullName()).Error(message); } public void Error(object message, Exception exception) { LogManager.GetLogger(GetCurrentMethodFullName()).Error(message, exception); } private string GetCurrentMethodFullName() { try { StackFrame frame; string str2; int num = 2; StackTrace trace = new StackTrace(); int length = trace.GetFrames().Length; do { frame = trace.GetFrame(num++); str2 = frame.GetMethod().DeclaringType.ToString(); } while (str2.EndsWith("Exception") && (num < length)); string name = frame.GetMethod().Name; return (str2 + "." + name); } catch { return null; } } public void Info(object message) { LogManager.GetLogger(GetCurrentMethodFullName()).Info(message); } public void Info(object message, Exception ex) { LogManager.GetLogger(GetCurrentMethodFullName()).Info(message, ex); } public void Warn(object message) { LogManager.GetLogger(GetCurrentMethodFullName()).Warn(message); } public void Warn(object message, Exception ex) { LogManager.GetLogger(GetCurrentMethodFullName()).Warn(message, ex); } } }