log4net.dll 可以在nuget程序包管理器中搜索下载
using System;using System.Collections.Generic;using System.Linq;using System.Web;using log4net;namespace Common{public class Logger{static ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);/// <summary>/// 记录异常/// </summary>/// <param name=\"message\"></param>/// <param name=\"stackTrace\"></param>public static void LogException(string message, string stackTrace){log.Debug(string.Format(\"Exception occured: {0}, stack trace: {1}\", message, stackTrace));}/// <summary>/// 记录日志/// </summary>/// <param name=\"info\"></param>public static void LogInfo(string info){log.Info(info);}}}
需要在AssemblyInfo.cs中加入
> [assembly: log4net.Config.XmlConfigurator(Watch = true)]
web.config中配置如下:
<log4net> .<appender name=\"FileAppender\" type=\"log4net.Appender.FileAppender\"><param name=\"File\" value=\"Logs\\\\\"/><param name=\"dateOn\" value=\"true\"/><param name=\"datePattern\" value=\"MM.dd.yyyy HH:mm\"/><param name=\"AppendToFile\" value=\"true\"/><layout type=\"log4net.Layout.PatternLayout\"><param name=\"ConversionPattern\" value=\"[%date] %message%newline\"/></layout><param name=\"lockingModel\" value=\"log4net.Appender.FileAppender+MinimalLock\"/><param name=\"StaticLogFileName\" value=\"false\"/><param name=\"rollingStyle\" value=\"Date\"/><param name=\"datePattern\" value=\"yyyyMMdd\"/></appender><root><level value=\"ALL\"/><appender-ref ref=\"FileAppender\"/></root></log4net>
`