配置:<configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <log4net> <!-- Define some output appenders --> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="test.txt"/> <appendToFile value="true"/> <!--最多10个日志备份文件--> <maxSizeRollBackups value="10"/> <!--每个文件最大1M--> <maximumFileSize value="1024KB"/> <!--回滚的格式按照大小--> <rollingStyle value="Size"/> <staticLogFileName value="true"/> <layout type="log4net.Layout.PatternLayout"> <!--date 当前的日期,thread线程,level日志级别,logger日志记录者,message错误信息,newline换行--> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/> </layout> </appender> <root> <level value="DEBUG"/> <appender-ref ref="RollingLogFileAppender"/> </root> </log4net>
页面:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>/ <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" style="height: 21px" Text="=" /> <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
后台:ILog log = LogManager.GetLogger("WebForm1.aspx"); protected void Button1_Click(object sender, EventArgs e) { //从配置文件读取log4net的配置,然后进行一个初始化工作。 log4net.Config.XmlConfigurator.Configure(); try { TextBox3.Text = (Convert.ToInt16(TextBox1.Text) / Convert.ToInt16(TextBox2.Text)).ToString(); } catch (Exception ex) { log.Error(ex.Message+"\n"+ex.StackTrace); return; } }