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