log4net下载地址:http://logging.apache.org/log4net/download.html 下载 incubating-log4net-1.2.10.zip 在webform开发环境中,log4net的配置必须放在网站根目录下的web.config中, 添加以下配置 <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> 上边的配置必须紧挨着<configuration>,否则会提示错误 "Error 49 D:\wwwroot\Default.aspx: ASP.NET runtime error: 每个配置文件中只允许存在一个 <configSections> 元素,并且,如果存在该元素,它还必须是根 <configuration> 元素的第一个子元素。 (D:\wwwroot\web.config line 18) D:\wwwroot\Default.aspx 1 1 D:\wwwroot\ " 然后添加 <!-- log4net配置声明 --> <log4net> <appender name="LogFileAppender" type="log4net.Appender.FileAppender"> <!-- Please make shure the ..\\Logs directory exists! --> <param name="File" value="Logs\\Log4Net.log"/> <!--<param name="AppendToFile" value="true"/>--> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/> </layout> </appender> <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender"> <to value="" /> <from value="" /> <subject value="" /> <smtpHost value="" /> <bufferSize value="512" /> <lossy value="true" /> <evaluator type="log4net.Core.LevelEvaluator"> <threshold value="WARN"/> </evaluator> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%newline%date [%thread] %-5level %logger [%property] - %message%newline%newline%newline" /> </layout> </appender> <logger name="File"> <level value="All" /> <appender-ref ref="LogFileAppender" /> </logger> <logger name="EmailLog"> <level value="All" /> <appender-ref ref="SmtpAppender" /> </logger> </log4net> 需要注意的是,多数网上的教程都提到,需要在AssemblyInfo.cs文件中, [assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)] 按照要求做了可是写不到文件里?怎么回事? 后来在CSDN中找到了答案,大意就是说,VS2005(我使用的VS2008)中,不会生成固定名称的DLL 不保证上行代码总被执行,建议款封装成静态类 按此说法,经实验表明是可取的!~以下为封装好了的Log类,在保证上述要求后,记得添加DLL引用, 然后直接Copy下边的代码就可以了. using System; using System.Collections.Generic; using System.Text; [assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)] namespace ChinaConstruct { #region log类 /// <summary> /// 公用类 /// </summary> public class Log { private static readonly log4net.ILog log = log4net.LogManager.GetLogger("File"); #region Debug /// <summary> /// 调试 /// </summary> /// <param name="message"></param> public static void Debug(object message) { log.Debug(message); } public static void Debug(object message, Exception exp) { log.Debug(message, exp); } public static void DebugFormat(string format, object arg0) { log.DebugFormat(format, arg0); } public static void DebugFormat(string format, params object[] args) { log.DebugFormat(format, args); } public static void DebugFormat(IFormatProvider provider, string format, params object[] args) { log.DebugFormat(provider, format, args); } public static void DebugFormat(string format, object arg0, object arg1) { log.DebugFormat(format, arg0, arg1); } public static void DebugFormat(string format, object arg0, object arg1, object arg2) { log.DebugFormat(format, arg0, arg1, arg2); } #endregion #region Error /// <summary> /// 错误 /// </summary> /// <param name="message"></param> public static void Error(object message) { log.Error(message); } public static void Error(object message, Exception exception) { log.Error(message, exception); } public static void ErrorFormat(string format, object arg0) { log.ErrorFormat(format, arg0); } public static void ErrorFormat(string format, params object[] args) { log.ErrorFormat(format, args); } public static void ErrorFormat(IFormatProvider provider, string format, params object[] args) { log.ErrorFormat(provider, format, args); } public static void ErrorFormat(string format, object arg0, object arg1) { log.ErrorFormat(format, arg0, arg1); } public static void ErrorFormat(string format, object arg0, object arg1, object arg2) { log.ErrorFormat(format, arg0, arg1, arg2); } #endregion #region Fatal /// <summary> /// 致命的,毁灭性的 /// </summary> /// <param name="message"></param> public static void Fatal(object message) { log.Fatal(message); } public static void Fatal(object message, Exception exception) { log.Fatal(message, exception); } public static void FatalFormat(string format, object arg0) { log.FatalFormat(format, arg0); } public static void FatalFormat(string format, params object[] args) { log.FatalFormat(format, args); } public static void FatalFormat(IFormatProvider provider, string format, params object[] args) { log.FatalFormat(provider, format, args); } public static void FatalFormat(string format, object arg0, object arg1) { log.FatalFormat(format, arg0, arg1); } public static void FatalFormat(string format, object arg0, object arg1, object arg2) { log.FatalFormat(format, arg0, arg1, arg2); } #endregion #region Info /// <summary> /// 信息 /// </summary> /// <param name="message"></param> public static void Info(object message) { log.Info(message); } public static void Info(object message, Exception exception) { log.Info(message, exception); } public static void InfoFormat(string format, object arg0) { log.InfoFormat(format, arg0); } public static void InfoFormat(string format, params object[] args) { log.InfoFormat(format, args); } public static void InfoFormat(IFormatProvider provider, string format, params object[] args) { log.InfoFormat(provider, format, args); } public static void InfoFormat(string format, object arg0, object arg1) { log.InfoFormat(format, arg0, arg1); } public static void InfoFormat(string format, object arg0, object arg1, object arg2) { log.InfoFormat(format, arg0, arg1, arg2); } #endregion #region Warn /// <summary> /// 警告,注意,通知 /// </summary> /// <param name="message"></param> public static void Warn(object message) { log.Warn(message); } public static void Warn(object message, Exception exception) { log.Warn(message, exception); } public static void WarnFormat(string format, object arg0) { log.WarnFormat(format, arg0); } public static void WarnFormat(string format, params object[] args) { log.WarnFormat(format, args); } public static void WarnFormat(IFormatProvider provider, string format, params object[] args) { log.WarnFormat(provider, format, args); } public static void WarnFormat(string format, object arg0, object arg1) { log.WarnFormat(format, arg0, arg1); } public static void WarnFormat(string format, object arg0, object arg1, object arg2) { log.WarnFormat(format, arg0, arg1, arg2); } #endregion } #endregion } |