基於log4net的協助類Log

來源:互聯網
上載者:User

標籤:color   web   cli   deb   display   技術分享   ack   null   names   

 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 Log11     {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             try35             {36                 StackFrame frame;37                 string str2;38                 int num = 2;39                 StackTrace trace = new StackTrace();40                 int length = trace.GetFrames().Length;41                 do42                 {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             catch51             {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 }
View Code

注意:如果要在項目中使用這個類一定先應用log4net

基於log4net的協助類Log

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.