Log4net: http://logging.apache.org/log4net/download.html Download incubating-log4net-1.2.10.zip In the webform development environment, log4net configurations must be placed in web. config under the root directory of the website, Add the following configurations <ConfigSections> <Section name = "log4net" type = "log4net. Config. Log4NetConfigurationSectionHandler, log4net"/> </ConfigSections> The preceding configuration must be placed next to <configuration>. Otherwise, an error is prompted. "Error 49 D: \ wwwroot \ Default. aspx: ASP. NET runtime error: Only one <configSections> element exists in each configuration file. If this element exists, it must also be the first child element of the root <configuration> element. (D: \ wwwroot \ web. config line 18) D: \ wwwroot \ Default. aspx 1 1 D: \ wwwroot \ "Then add <! -- Log4net configuration Declaration --> <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] %-5 p % c % m % n"/> </Layout> </Appender> <Appender name = "SmtpAppender" type = "log4net. Appender. SmtpAppender"> <To value = ""/> <From value = ""/> <Subject value = ""/> <SmtpHost value = ""/> <BufferSize value = "512" type = "codeph" text = "/codeph"/> <Lossy value = "true"/> <Evaluator type = "log4net. Core. LevelEvaluator"> <Threshold value = "WARN"/> </Evaluator> <Layout type = "log4net. Layout. PatternLayout"> <ConversionPattern value = "% newline % date [% thread] %-5 level % logger [% property]-% message % newline"/> </Layout> </Appender> <Logger name = "File"> <Level value = "All"/> <Appender-ref = "LogFileAppender"/> </Logger> <Logger name = "EmailLog"> <Level value = "All"/> <Appender-ref = "SmtpAppender"/> </Logger> </Log4net>
It should be noted that, as mentioned in most online tutorials, the files must be in the AssemblyInfo. cs file, [Assembly: log4net. Config. XmlConfigurator (ConfigFile = "Web. config", Watch = true)] I did it as required, but I couldn't write it into the file? What's going on? Later, I found the answer in CSDN, which means that no DLL with a fixed name will be generated in VS2005 (My VS2008 ). The uplink code is not always executed. We recommend that You encapsulate the code into a static class. According to this, experiments show that it is desirable !~ The following are encapsulated Log classes. Remember to add DLL references after the preceding requirements are ensured, Then Copy the code below. Using System; Using System. Collections. Generic; Using System. Text; [Assembly: log4net. Config. XmlConfigurator (ConfigFile = "Web. config", Watch = true)] Namespace ChinaConstruct { # Region log class /// <Summary> /// Public class /// </Summary> Public class Log { Private static readonly log4net. ILog log = log4net. LogManager. GetLogger ("File "); # Region Debug /// <Summary> /// Debug /// </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> /// Error /// </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> /// Fatal and devastating /// </Summary> /// <Param name = "message"> </param> Public static void fatal (Object message) { Log. Fatal (Message ); } Public static void fatal (Object message, 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> /// Information /// </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> /// Warning, note, notification /// </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 }
|