Log4Net帮助类

时间:2023-03-10 01:10:04
Log4Net帮助类

工具类

using System;
using System.Diagnostics;
using log4net; namespace Trumgu_BI_PF.Util
{
public class Log4NetHelper
{
public static void Debug(object message)
{
LogManager.GetLogger(GetCurrentMethodFullName()).Debug(message);
} public static void Debug(object message, Exception ex)
{
LogManager.GetLogger(GetCurrentMethodFullName()).Debug(message, ex);
} public static void Error(object message)
{
LogManager.GetLogger(GetCurrentMethodFullName()).Error(message);
} public static void Error(object message, Exception exception)
{
LogManager.GetLogger(GetCurrentMethodFullName()).Error(message, exception);
} private static string GetCurrentMethodFullName()
{
try
{
StackFrame frame;
string str2;
int num = ;
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 static void Info(object message)
{
LogManager.GetLogger(GetCurrentMethodFullName()).Info(message);
} public static void Info(object message, Exception ex)
{
LogManager.GetLogger(GetCurrentMethodFullName()).Info(message, ex);
} public static void Warn(object message)
{
LogManager.GetLogger(GetCurrentMethodFullName()).Warn(message);
} public static void Warn(object message, Exception ex)
{
LogManager.GetLogger(GetCurrentMethodFullName()).Warn(message, ex);
}
}
}

使用方法

Util.Log4NetHelper.Info("-----------------执行统计脚本并群发邮件服务 Start--------------------");
Util.Log4NetHelper.Error("NPOI生成Excle发成异常!", ex);