![](http://images.cnblogs.com/cnblogs_com/downmoon/121756/r_sunafa02.jpg)
log4net是一个开源的错误日志记录项目,易用性强,源自log4j,品质值得信赖。下面汇报一下我的安装部署log4net到MS sql server的经验。
log4net是一个开源的错误日志记录项目,易用性强,源自log4j,品质值得信赖。
下面汇报一下我的安装部署log4net到MS sql server的经验。
一、首先是下载log4net最新版
地址:http://logging.apache.org/log4net/
直接引用dll也行,我是把下载的源码在Vs2005下编译成log4net.dll
二、在你的web主项目(比如MainWeb.csproj)添加对log4net.dll的引用,并在相应的Globall.cs文件中的Application_Start事件中写入
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
protected void Application_Start(Object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{/**/////
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo("log4net.config"));
}
在这个项目对应的Assembly.cs文件中添加一行
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
这里有个地方需要注意
如果我的主项目是纯粹的代码分离后的aspx\ascx文件, 不含cs文件,那么中需要在饱含cs文件的项目(比如common.web.csproj)文件中添加这两行代码
举个例子
解决方案如下:
MySolution.Sln
|-----common.Web.csproj(包含cs文件)
|-----Business1.Web.csproj(包含cs文件,继承自common.Web.csproj)
|-----Business********.Web.csproj(包含cs文件,继承自common.Web.csproj)
|-----BusinessX.Web.csproj(包含cs文件,继承自common.Web.csproj)
|-----MainWeb.csproj(不包含cs文件)
此时, 只需要在 common.Web.csproj中做以上动作就可以了!其他项目只需要引用 dll即可。
第三步:添加logn4net.config在主项目根目录下:与web.config同路径,之所以不写入web.config文件, 是因为这样可以单独修改调试部署,也就是非侵入式安装的思想。呵呵。
内容大概如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<!--Log4net Begin by Tony 2008.11.20-->
<log4net>
<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
<bufferSize value="10" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="DATABASE=DBErrorLogs;SERVER=myserver;UID=user;PWD=pwdpwd;Connect Timeout=15;" />
<commandText value="INSERT INTO ErrorLog ([dtDate],[sThread],[sLevel],[sLogger],[sMessage],[sException]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
<parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@thread" />
<dbType value="String" />
<size value="100" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%t" />
</layout>
</parameter>
<parameter>
<parameterName value="@log_level" />
<dbType value="String" />
<size value="200" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%p" />
</layout>
</parameter>
<parameter>
<parameterName value="@logger" />
<dbType value="String" />
<size value="500" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="3000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%m" />
</layout>
</parameter>
<parameter>
<parameterName value="@exception" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
<!-- setup the root category, add the appenders and set the default level -->
<root>
<level value="WARN"/>
<level value="INFO"/>
<level value="DEBUG"/>
<level value="FINE"/>
<appender-ref ref="ADONetAppender" />
</root>
<!-- specify the level for some specific categories -->
<logger name="iNotes">
<level value="WARN"/>
<level value="INFO"/>
<level value="DEBUG"/>
<level value="FINE"/>
<appender-ref ref="ADONetAppender"/>
</logger>
<logger name="StellaLogger">
<level value="ALL"/>
<appender-ref ref="AdoNetAppender" />
</logger>
</log4net>
<!--Log4net End-->
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</configuration>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
具体的可以看看官方文档
http://logging.apache.org/log4net/release/sdk/index.html
基本完成了!需要注意的一个参数
<bufferSize value="10" />,表示是记录10 条到缓冲区,满10条后再写入SQL server
调试时可以改为1,实际应用建议为100
数据库生成脚本
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
SET ANSI_NULLS ON
GO
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET QUOTED_IDENTIFIER ON
GO
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET ANSI_PADDING ON
GO
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
CREATE TABLE [dbo].[ErrorLog](
[nId] [bigint] IDENTITY(1,1) NOT NULL,
[dtDate] [datetime] NOT NULL,
[sThread] [varchar](100) NOT NULL,
[sLevel] [varchar](200) NOT NULL,
[sLogger] [varchar](500) NOT NULL,
[sMessage] [varchar](3000) NOT NULL,
[sException] [varchar](4000) NULL,
CONSTRAINT [PK_ErrorLog] PRIMARY KEY CLUSTERED
(
[nId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
GO
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET ANSI_PADDING OFF
GO
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ALTER TABLE [dbo].[ErrorLog] ADD DEFAULT (getdate()) FOR [dtDate]
GO
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
第四步:
在需要记录的每个类中添加如下代码
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
private static log4net.ILog myLogger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
给出一个例子
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
private void TestLog()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
}
catch (Exception ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Loghandle by Tony 2008.11.21#region Loghandle by Tony 2008.11.21
string loginid = EmptyString;
//if (user != null) { loginid = user.LoginID; }
myLogger.Error(GetErrorMessage(loginid, 1), ex);
//Debug("系统已记录错误,请联系管理员!"); return;
//Debug(ErrorHandle.GetErrorInfoByID(999), this.ClientID); return;
#endregion
}
}
记录的样式如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<table><tr><td>nId</td>
<td>dtDate</td> <td>sThread</td> <td>sLevel</td> <td>sLogger</td> <td>sMessage</td> <td>sException</td>
</tr>
<tr><td>1 </td>
<td>2008-11-21 11:03:20.470</td> <td>1904</td> <td>ERROR Agronet.Product.Web.UI.Controls.Vegnet.Detail</td> <td>10.103.33.10</td> <td>System.OverflowException: 值对于 Int16 太大或太小。</td> <td> ---> System.OverflowException: 值对于 Int32 太大或太小。 在 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) 在 System.Int16.Parse(String s, NumberStyles style, NumberFormatInfo info) --- 内部异常堆栈跟踪的结尾 --- 在 System.Int16.Parse(String s, NumberStyles style, NumberFormatInfo info) 在 System.Int16.Parse(String s) 在 Agronet.Product.Web.UI.Controls.Vegnet.Detail.BindList() 位置 P:\Test\Web\UI\Controls\Vegnet\Detail.cs:行号 141</td>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</tr></table>
OK! 结束,
Any question ,Please mail to tony(邀月)3w@live.cn