(二)使用单独的配置文件,不放在web.config里
1.新建一website.
2.添加一个类库工程,取名LogNet
3. 为LogNet工程引用log4net.dll
4. 双击打开AssemblyInfo.cs在最后添加
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
using
System;
using
System.Collections.Generic;
using
System.Text;
namespace
LogNet
{
public class LogBLL
{
public static void debug(string message)
{
log4net.ILog log = log4net.LogManager.GetLogger("Test");
if (log.IsDebugEnabled)
{
log.Debug(message);
}
log = null;
}
public static void error(string message)
{
log4net.ILog log = log4net.LogManager.GetLogger("Test");
if (log.IsErrorEnabled)
{
log.Error(message);
}
log = null;
}
public static void fatal(string message)
{
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
log4net.ILog log = log4net.LogManager.GetLogger("Test");
if (log.IsFatalEnabled)
{
log.Fatal(message);
}
log = null;
}
public static void info(string message)
{
log4net.ILog log = log4net.LogManager.GetLogger("Test");
if (log.IsInfoEnabled)
{
log.Info(message);
}
log = null;
}
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
public static void warn(string message)
{
log4net.ILog log = log4net.LogManager.GetLogger("Test");
if (log.IsWarnEnabled)
{
log.Warn(message);
}
log = null;
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
6.在web站点根目录下添加一个log4net.config文件,内容如下
8.为web引入LogNet工程bin目录下的LogNet.dll
9.在default.aspx.cs里写入如下代码
10.运行,将在web目录下的log目录下看到日志文件
----------------------header--------------------------
2007-11-24 12:16:24,171 [4] DEBUG LiveBookings - 测试信息
2007-11-24 12:16:46,312 [4] DEBUG LiveBookings - 测试信息
----------------------footer--------------------------
1.新建一website.
2.添加一个类库工程,取名LogNet
![](https://i-blog.csdnimg.cn/blog_migrate/292ca5b17949679db375ec3992d2cc01.jpeg)
3. 为LogNet工程引用log4net.dll
4. 双击打开AssemblyInfo.cs在最后添加
[assembly: log4net.Config.XmlConfigurator(ConfigFile
=
"
log4net.config
"
, Watch
=
true
)]
5. 为LogNet工程添加一个logBLL.cs文件,我们把日志调用封装到这一层
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/eec4c0236afc26744c9c4e910bc34958.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/eec4c0236afc26744c9c4e910bc34958.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/eec4c0236afc26744c9c4e910bc34958.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/eec4c0236afc26744c9c4e910bc34958.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/eec4c0236afc26744c9c4e910bc34958.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/eec4c0236afc26744c9c4e910bc34958.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/eec4c0236afc26744c9c4e910bc34958.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/eec4c0236afc26744c9c4e910bc34958.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/eec4c0236afc26744c9c4e910bc34958.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/eec4c0236afc26744c9c4e910bc34958.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/eec4c0236afc26744c9c4e910bc34958.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4fd96b3cf02f4c7b5c8964ac8167f7af.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
6.在web站点根目录下添加一个log4net.config文件,内容如下
<?
xml version="1.0" encoding="utf-8"
?>
< configuration >
< configSections >
< section name ="log4net" type ="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</ configSections >
< log4net >
< root >
<!--
<level value="ALL" />
<appender-ref ref="rootFile" />
-->
</ root >
< logger name ="Test" >
< level value ="DEBUG" />
< appender-ref ref ="rollingFile" />
</ logger >
< appender name ="rollingFile" type ="log4net.Appender.RollingFileAppender,log4net" >
< param name ="File" type ="" value ="log/" />
< param name ="AppendToFile" value ="true" />
< param name ="RollingStyle" value ="Date" />
< param name ="DatePattern" value ="yyyyMMdd" />
< param name ="StaticLogFileName" value ="false" />
< layout type ="log4net.Layout.PatternLayout,log4net" >
< param name ="ConversionPattern" value ="%d [%t] %-5p %c - %m%n" />
< param name ="Header" value =" ----------------------header-------------- " />
< param name ="Footer" value =" ----------------------footer-------------- " />
</ layout >
</ appender >
</ log4net >
</ configuration >
7.编译LogNet工程
< configuration >
< configSections >
< section name ="log4net" type ="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</ configSections >
< log4net >
< root >
<!--
<level value="ALL" />
<appender-ref ref="rootFile" />
-->
</ root >
< logger name ="Test" >
< level value ="DEBUG" />
< appender-ref ref ="rollingFile" />
</ logger >
< appender name ="rollingFile" type ="log4net.Appender.RollingFileAppender,log4net" >
< param name ="File" type ="" value ="log/" />
< param name ="AppendToFile" value ="true" />
< param name ="RollingStyle" value ="Date" />
< param name ="DatePattern" value ="yyyyMMdd" />
< param name ="StaticLogFileName" value ="false" />
< layout type ="log4net.Layout.PatternLayout,log4net" >
< param name ="ConversionPattern" value ="%d [%t] %-5p %c - %m%n" />
< param name ="Header" value =" ----------------------header-------------- " />
< param name ="Footer" value =" ----------------------footer-------------- " />
</ layout >
</ appender >
</ log4net >
</ configuration >
8.为web引入LogNet工程bin目录下的LogNet.dll
9.在default.aspx.cs里写入如下代码
using
System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using LogNet; // 引用程序集
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
if ( ! Page.IsPostBack)
{
LogBLL.debug( " 测试信息 " );
}
}
}
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using LogNet; // 引用程序集
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
if ( ! Page.IsPostBack)
{
LogBLL.debug( " 测试信息 " );
}
}
}
10.运行,将在web目录下的log目录下看到日志文件
----------------------header--------------------------
2007-11-24 12:16:24,171 [4] DEBUG LiveBookings - 测试信息
2007-11-24 12:16:46,312 [4] DEBUG LiveBookings - 测试信息
----------------------footer--------------------------
本文源码下载: 点击这里下载
本文转自敏捷的水博客园博客,原文链接http://www.cnblogs.com/cnblogsfans/archive/2007/11/24/971129.html如需转载请自行联系原作者
王德水