1. First, configure the inside of the Webconfig
<configuration> <configSections> <section name= "log4net" type= "log4net. Config.log4netconfigurationsectionhandler,log4net "/> </configSections> <log4net> <root> <priority value= "All"/> <appender-ref ref= "Traceappender"/> <appender-ref ref= " Consoleappender "/> <appender-ref ref=" Rollinglogfileappender "/> </root> <appender name= "Traceappender" type= "log4net. Appender.traceappender "> <layout type=" log4net. Layout.patternlayout "> <conversionpattern value="%date [%thread]% -5level%logger [%PROPERTY{NDC}]-%message%newline "/> </layout> </appender>   <appender name= "Consoleappender" type= "log4net". Appender.consoleappender "> <layout type=" log4net. Layout.patternlayout "> <conversionpattern value="%date [%thread]% -5level%logger [%PROPERTY{NDC}]-%message%newline "/> </layout> </appender> <appender name= "Rollinglogfileappender" type= "log4net. Appender.rollingfileappender "> <file value=" log/log/"/> <appendtofile value= "true"/> <rollingstyle value= "Date"/> & nbsp; <datepattern value= ". YyyyMMdd '. html '"/> < Staticlogfilename value= "false"/> <layout type= "Log4net". Layout.patternlayout "> <conversionpattErn value= "%d [%t]%-5p%c-%m%n"/> </layout> </appender& Gt </log4net>
2. Then build a Loghelper class
Using System; Using System.Data; Using System.Configuration; Using System.Web; Using System.Web.Security; Using System.Web.UI; Using System.Web.UI.WebControls; Using System.Web.UI.WebControls.WebParts; Using System.Web.UI.HtmlControls; Using System.IO; Using Log4net; <summary>///loghelp Summary description///</summary> public class Loghelp {public Loghelp () {// TODO: Add constructor logic//} here
Private Const string log_repository = "Default"; This should likely is set in the Web Config. private static ILog M_log; public static void Init () {log4net. Config.XmlConfigurator.Configure (); }
private static void Dolog (String message, Logmessagetype MessageType, Exception ex, type type) {M_log = Logm Anager. GetLogger (type);
Switch (messagetype) {case LogMessageType.Debug:LogHelp.m_log. DEBUG (message, ex); Break
Case LogMessageType.Info:LogHelp.m_log. Info (message, ex); Break
Case LogMessageType.Warn:LogHelp.m_log. Warn (message, ex); Break
Case LogMessageType.Error:LogHelp.m_log. ERROR (message, ex); Break
Case Logmessagetype.fatal: Loghelp.m_log. Fatal (message, ex); break; } } /* Log level/ Public enum Logmessagetype { ///<summary> & nbsp; ///Debugging ///</summary> Debug, ///<summary> ///Information ///</summary> Info, ///<summary> ///warning ///</summary> Warn ///<summary> ///error ///</summary> Error, ///<summary> ///fatal error ///</summary> Fatal }
public static void Write (String message, Logmessagetype messagetype) {dolog (message, MessageType, NULL , Type.GetType ("System.Object")); }
public static void Write (String message, Logmessagetype MessageType, type type) {dolog (message, MessageType, null, type); }
public static void Write (String message, Logmessagetype MessageType, Exception ex) {dolog (message, Messagetyp E, ex, Type.GetType ("System.Object")); }
public static void Write (String message, Logmessagetype MessageType, Exception ex, type type) {Dolog (Message, MessageType, ex, type); }
public static void Assert (bool condition, string message) {ASSERT (condition, message, Type.GetType ("System.ob Ject ")); }
public static void Assert (bool condition, string message, type type) {if (condition = false) Wri TE (message, logmessagetype.info); } 3 finally written in global
<%@ application Language= "C #"%> <%@ Import namespace= system.diagnostics "%>" <script runat= "Server" >
void Application_Start (object sender, EventArgs e) {loghelp.init (); Loghelp.write ("Site program runs ...<br>", LogHelp.LogMessageType.Info, this.) GetType ()); } void Application_End (object sender, EventArgs e) {} void Application_Error (Object sen Der, EventArgs E) {//code to run when an unhandled error occurs Exception err = Server.GetLastError ();
if (err is Exception) { //stringbuilder sb = new StringBuilder ();
//if (request.islocal) // sb. Appendline (Err. Message.tostring ()); //else // sb. Appendline (String. Format ("The System program has an exception. <BR/> Error message: {0} ", err)); //server.clearerror (); //apperror.writeerror ( AppError.ErrorType.Error, sb. ToString ()); try { loghelp.write ("Exception:", LoghelP.logmessagetype.error, err, this. GetType ()); } catch { //Log Write Failed ... } } }
void Session_Start (object sender, EventArgs e) { //code to run at the start of a new session //application.lock (); //application["Count" = Convert.ToInt32 (application["Count"]) + 1; //sqlparameter[] param ={new SqlParameter ("@counter", application["Count "]. ToString ())}; //string procname = "dbo." Updatecounter "; //database.executequery (Procname,param); //application.unlock (); }
void Session_End (object sender, EventArgs e) {//code that runs at the end of the session. Note: The Session_End event is raised only if the sessionstate mode in the Web.config file is set to//InProc. If the session mode is set to StateServer//or SQL Server, the event is not raised.
} </script>