控制台:
App.config,log4的配置,同时要在E:\MyWork\log4日志测试\ 建立对应文文件夹存放log文件
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> <!--log4net组件--> </configSections> <log4net> <root> <!--从高到低的七个级别:OFF,FATAL,RROR,WARN,INFO,DEBUG,ALL--> <level value="ALL"/> </root> <!--配置程序报错专用--> <logger name="testLog"> <level value="ALL"/> <appender-ref ref="testLogApp"/> </logger> <appender name="testLogApp" type="log4net.Appender.RollingFileAppender, log4net"> <!--日志路径--> <param name="File" value="E:\MyWork\log4日志测试\"/> <!--日期为滚动类型(每天产生一个日志文件)--> <param name="RollingStyle" value="Date"/> <!--追加方式--> <param name="AppendToFile" value="true"/> <!--日志文件名--> <param name="DatePattern" value="yyyy-MM-dd".log""/> <!--关闭固定文件方式--> <param name="StaticLogFileName" value="false"/> <!--记录格式--> <layout type="log4net.Layout.PatternLayout, log4net"> <!--%m:消息内容--> <!--%n:换行--> <!--%d:输出时间--> <!--%p:级别--> <!--%c:类名--> <!--%F:文件名--> <param name="ConversionPattern" value="[%d] %p - %m%n"/> </layout> <!--过滤器--> <filter type="log4net.Filter.LevelRangeFilter, log4net"> <param name="LevelMin" value="ALL"/> <param name="LevelMax" value="OFF"/> </filter> </appender> </log4net> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>
Program:
static void Main(string[] args) { log4net.Config.DOMConfigurator.Configure(); //不加这句话日志不会生成 log4net.ILog testLog = log4net.LogManager.GetLogger("testLog"); try { throw new ApplicationException("测试log4,我是控制台抛出的异常!!"); } catch (Exception ex) { testLog.Info("******************************* begin static void Main(string[] args)程序异常 ********************************************"); testLog.Info(ex); testLog.Info("******************************* end static void Main(string[] args)程序异常 ********************************************"); } }
结果:
webform程序配置
web.config
<?xml version="1.0" encoding="utf-8"?> <!-- 有关如何配置 ASP.NET 应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> <!--log4net组件--> </configSections> <log4net> <root> <!--从高到低的七个级别:OFF,FATAL,RROR,WARN,INFO,DEBUG,ALL--> <level value="ALL"/> </root> <!--配置程序报错专用--> <logger name="testLog"> <level value="ALL"/> <appender-ref ref="testLogApp"/> </logger> <appender name="testLogApp" type="log4net.Appender.RollingFileAppender, log4net"> <!--日志路径--> <param name="File" value="E:\MyWork\log4日志测试\"/> <!--日期为滚动类型(每天产生一个日志文件)--> <param name="RollingStyle" value="Date"/> <!--追加方式--> <param name="AppendToFile" value="true"/> <!--日志文件名--> <param name="DatePattern" value="yyyy-MM-dd".log""/> <!--关闭固定文件方式--> <param name="StaticLogFileName" value="false"/> <!--记录格式--> <layout type="log4net.Layout.PatternLayout, log4net"> <!--%m:消息内容--> <!--%n:换行--> <!--%d:输出时间--> <!--%p:级别--> <!--%c:类名--> <!--%F:文件名--> <param name="ConversionPattern" value="[%d] %p - %m%n"/> </layout> <!--过滤器--> <filter type="log4net.Filter.LevelRangeFilter, log4net"> <param name="LevelMin" value="ALL"/> <param name="LevelMax" value="OFF"/> </filter> </appender> </log4net> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> </system.web> </configuration>
log4net.Config.DOMConfigurator.Configure();//这句注册的语句最好写在Global.asax全局访问类里,webform程序不写在里面每个页面都要写。
Global:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Security; using System.Web.SessionState; namespace log4.test.Webform { public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { log4net.Config.DOMConfigurator.Configure(); } } }
WebForm1.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="log4.test.Webform.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="btnsave" runat="server" Text="testLog4" OnClick="btnsave_Click" /> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace log4.test.Webform { public partial class WebForm1 : System.Web.UI.Page { protected readonly log4net.ILog testLog = log4net.LogManager.GetLogger("testLog"); protected void Page_Load(object sender, EventArgs e) { } protected void btnsave_Click(object sender, EventArgs e) { try { throw new ApplicationException("测试log4,我是webform抛出的异常!!"); } catch (Exception ex) { testLog.Info("******************************* begin btnsave_Click(object sender, EventArgs e)点击事件异常 ********************************************"); testLog.Info(ex); testLog.Info("******************************* end btnsave_Click(object sender, EventArgs e)点击事件异常 ********************************************"); } } } }
时间: 2024-10-12 05:48:49