zoukankan      html  css  js  c++  java
  • 基于log4net的帮助类Log

     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 }
    View Code

    注意:如果要在项目中使用这个类一定先应用log4net

  • 相关阅读:
    CF997C Sky Full of Stars
    LOJ6160 二分图染色
    AT4996 [AGC034F] RNG and XOR
    AT4119 [ARC096C] Everything on It
    20200701线性代数概率期望练习
    SNOI2020 LOJ3326 字符串
    SNOI2020 LOJ3323 生成树
    SNOI2020 LOJ3324 取石子
    Gym-102576A Bags of Candies
    Gym-102576H Lighthouses
  • 原文地址:https://www.cnblogs.com/bindot/p/log4nethelper.html
Copyright © 2011-2022 走看看