1 using log4net; 2 using System; 3 using System.Collections.Generic; 4 using System.Diagnostics; 5 using System.Linq; 6 using System.Web; 7 8 namespace TryLittle.Helper 9 { 10 public static class Log 11 { 12 public static void Debug(object message) 13 { 14 LogManager.GetLogger(GetCurrentMethodFullName()).Debug(message); 15 } 16 17 public static void Debug(object message, Exception ex) 18 { 19 LogManager.GetLogger(GetCurrentMethodFullName()).Debug(message, ex); 20 } 21 22 public static void Error(object message) 23 { 24 LogManager.GetLogger(GetCurrentMethodFullName()).Error(message); 25 } 26 27 public static void Error(object message, Exception exception) 28 { 29 LogManager.GetLogger(GetCurrentMethodFullName()).Error(message, exception); 30 } 31 32 private static string GetCurrentMethodFullName() 33 { 34 try 35 { 36 StackFrame frame; 37 string str2; 38 int num = 2; 39 StackTrace trace = new StackTrace(); 40 int length = trace.GetFrames().Length; 41 do 42 { 43 frame = trace.GetFrame(num++); 44 str2 = frame.GetMethod().DeclaringType.ToString(); 45 } 46 while (str2.EndsWith("Exception") && (num < length)); 47 string name = frame.GetMethod().Name; 48 return (str2 + "." + name); 49 } 50 catch 51 { 52 return null; 53 } 54 } 55 56 public static void Info(object message) 57 { 58 LogManager.GetLogger(GetCurrentMethodFullName()).Info(message); 59 } 60 61 public static void Info(object message, Exception ex) 62 { 63 LogManager.GetLogger(GetCurrentMethodFullName()).Info(message, ex); 64 } 65 66 public static void Warn(object message) 67 { 68 LogManager.GetLogger(GetCurrentMethodFullName()).Warn(message); 69 } 70 71 public static void Warn(object message, Exception ex) 72 { 73 LogManager.GetLogger(GetCurrentMethodFullName()).Warn(message, ex); 74 } 75 76 } 77 }
注意:如果要在项目中使用这个类一定先应用log4net
时间: 2024-10-15 14:05:17