直接上图片。下面粘贴代码。 QQ群601122412
1、首先log4的xml放到根目录。
2、Global 增加代码。直接复制就能用。
//log4
var log4netPath = System.Web.Hosting.HostingEnvironment.MapPath("~/log4net.cfg.xml");
var log4netFileInfo = new FileInfo(log4netPath);
XmlConfigurator.Configure(log4netFileInfo);
3、添加一个common。这个看自己。添加一个class。把log.cs添加进去。
这个是log类的源码。命名空间啥的自己改改就行了。
/*
源码己托管:http://git.oschina.net/kuiyu/dotnetcodes
*/
using log4net;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace ProjectP.common
{
/// <summary>
/// Log4.Net 日志帮助类
/// </summary>
public static class Log
{
public static void Debug(object message)
{
LogManager.GetLogger(ProjectP.common.Log.GetCurrentMethodFullName()).Debug(message);
}
public static void Debug(object message, Exception ex)
{
LogManager.GetLogger(ProjectP.common.Log.GetCurrentMethodFullName()).Debug(message, ex);
}
public static void Error(object message)
{
LogManager.GetLogger(ProjectP.common.Log.GetCurrentMethodFullName()).Error(message);
}
public static void Error(object message, Exception exception)
{
LogManager.GetLogger(ProjectP.common.Log.GetCurrentMethodFullName()).Error(message, exception);
}
public static void Info(object message)
{
LogManager.GetLogger(ProjectP.common.Log.GetCurrentMethodFullName()).Info(message);
}
public static void Info(object message, Exception ex)
{
LogManager.GetLogger(ProjectP.common.Log.GetCurrentMethodFullName()).Info(message, ex);
}
public static void Warn(object message)
{
LogManager.GetLogger(ProjectP.common.Log.GetCurrentMethodFullName()).Warn(message);
}
public static void Warn(object message, Exception ex)
{
LogManager.GetLogger(ProjectP.common.Log.GetCurrentMethodFullName()).Warn(message, ex);
}
private static string GetCurrentMethodFullName()
{
StackFrame frame;
string str;
string str1;
bool flag;
try
{
int num = 2;
StackTrace stackTrace = new StackTrace();
int length = stackTrace.GetFrames().Length;
do
{
int num1 = num;
num = num1 + 1;
frame = stackTrace.GetFrame(num1);
str = frame.GetMethod().DeclaringType.ToString();
flag = (!str.EndsWith("Exception") ? false : num < length);
}
while (flag);
string name = frame.GetMethod().Name;
str1 = string.Concat(str, ".", name);
}
catch
{
str1 = null;
}
return str1;
}
}
}
4、增加一个引用。log4net.dll文件。网上可以下载。要是没有的话可以找我要。私人QQ1360651533 QQ群601122412
5、最后用法再最后一张图。