原创|其它|编辑:郝浩|2009-08-24 10:18:36.000|阅读 833 次
概述:为了实践项目驱动的ExtAspNet开发过程,最近我启动了另外一个开源项目 - AppBox。AppBox项目使用ExtAspNet作为前台展现层,SubSonic作为ORM层,SqlServer2005作为数据库,在Asp.Net2.0基础之上实现一个企业综合管理系统所必须的基础组件。
# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>
前言
为了实践项目驱动的开发过程,最近我启动了另外一个开源项目 - 。
AppBox项目使用ExtAspNet作为前台展现层,SubSonic作为ORM层,SqlServer2005作为数据库,在Asp.Net2.0基础之上实现一个企业综合管理系统所必须的基础组件。
包括用户管理,菜单管理,权限管理,组织结构管理等各个部分,虽然AppBox不是给最终用户使用的,但是可以作为开发人员搭建网站的一个框架,同时在项目中遇到的控件会优先在ExtAspNet中实现。
由于在AppBox中使用了log4net作为日志记录组件,所以这篇文章就来分享一下log4net的配置和使用。
log4net配置
1. 首先到 //logging.apache.org/ 下载最新的log4net v1.2.10。
2. 建立数据库表
01.
CREATE
TABLE
[dbo].[Log] (
02.
[Id] [
int
] IDENTITY (1, 1)
NOT
NULL
,
03.
[
Date
] [datetime]
NOT
NULL
,
04.
[Thread] [
varchar
] (255)
NOT
NULL
,
05.
[
Level
] [
varchar
] (50)
NOT
NULL
,
06.
[Logger] [
varchar
] (255)
NOT
NULL
,
07.
[Message] [
varchar
] (4000)
NOT
NULL
,
08.
[Exception] [
varchar
] (2000)
NULL
09.
)
3. 在网站根目录添加log4net.config文件
01.
<
log4net
>
02.
<
root
>
03.
<
level
value
=
"ALL"
/>
04.
<
appender-ref
ref
=
"AdoNetAppender"
/>
05.
<
appender-ref
ref
=
"RollingFileAppender"
/>
06.
</
root
>
07.
<
appender
name
=
"RollingFileAppender"
type
=
"log4net.Appender.RollingFileAppender,log4net"
>
08.
<
param
name
=
"File"
value
=
"log\log.config"
/>
09.
<
param
name
=
"AppendToFile"
value
=
"true"
/>
10.
<
param
name
=
"MaxSizeRollBackups"
value
=
"10"
/>
11.
<
param
name
=
"MaximumFileSize"
value
=
"5MB"
/>
12.
<
param
name
=
"RollingStyle"
value
=
"Size"
/>
13.
<
param
name
=
"StaticLogFileName"
value
=
"true"
/>
14.
<
layout
type
=
"log4net.Layout.PatternLayout,log4net"
>
15.
<
param
name
=
"ConversionPattern"
value
=
"%d [%t] %-5p %c [%x] - %m%n"
/>
16.
</
layout
>
17.
</
appender
>
18.
<
appender
name
=
"AdoNetAppender"
type
=
"log4net.Appender.AdoNetAppender"
>
19.
<
bufferSize
value
=
"0"
/>
20.
<
connectionType
value
=
"System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral,PublicKeyToken=b77a5c561934e089"
/>
21.
<
connectionString
value
=
"Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=AppBox;Data Source=."
/>
22.
<
commandText
value
=
"insert into X_Log(DATETIME,THREAD,LOG_LEVEL,LOGGER,MESSAGE,EXCEPTION) values (@log_date,@thread,@log_level,@logger,@message,@exception)"
/>
23.
<
parameter
>
24.
<
parameterName
value
=
"@log_date"
/>
25.
<
dbType
value
=
"DateTime"
/>
26.
<
layout
type
=
"log4net.Layout.RawTimeStampLayout"
/>
27.
</
parameter
>
28.
<
parameter
>
29.
<
parameterName
value
=
"@thread"
/>
30.
<
dbType
value
=
"String"
/>
31.
<
size
value
=
"255"
/>
32.
<
layout
type
=
"log4net.Layout.PatternLayout"
>
33.
<
conversionPattern
value
=
"%thread"
/>
34.
</
layout
>
35.
</
parameter
>
36.
<
parameter
>
37.
<
parameterName
value
=
"@log_level"
/>
38.
<
dbType
value
=
"String"
/>
39.
<
size
value
=
"50"
/>
40.
<
layout
type
=
"log4net.Layout.PatternLayout"
>
41.
<
conversionPattern
value
=
"%level"
/>
42.
</
layout
>
43.
</
parameter
>
44.
<
parameter
>
45.
<
parameterName
value
=
"@logger"
/>
46.
<
dbType
value
=
"String"
/>
47.
<
size
value
=
"255"
/>
48.
<
layout
type
=
"log4net.Layout.PatternLayout"
>
49.
<
conversionPattern
value
=
"%logger"
/>
50.
</
layout
>
51.
</
parameter
>
52.
<
parameter
>
53.
<
parameterName
value
=
"@message"
/>
54.
<
dbType
value
=
"String"
/>
55.
<
size
value
=
"4000"
/>
56.
<
layout
type
=
"log4net.Layout.PatternLayout"
>
57.
<
conversionPattern
value
=
"%message"
/>
58.
</
layout
>
59.
</
parameter
>
60.
<
parameter
>
61.
<
parameterName
value
=
"@exception"
/>
62.
<
dbType
value
=
"String"
/>
63.
<
size
value
=
"2000"
/>
64.
<
layout
type
=
"log4net.Layout.ExceptionLayout"
/>
65.
</
parameter
>
66.
</
appender
>
67.
</
log4net
>
注:这里我们使用了两种类型的日志记录方式,文件和数据库。
文件保存在网站根目录下的 log\log.config ,必须保证Asp.Net服务进程对此文件夹有写权限,否则不能写入文件并且没有任何提示。
比如在WindowXP下需要设置 ASPNET (Windows2003不是这个名称,可以Google一下) 对此文件夹的写权限。
同时注意我们使用log.config而不是log.txt,是为了防止匿名用户非法下载系统日志。
在数据库配置上也有个小技巧,我们设置了 bufferSize value="0",也就是说产生一条日志就写到数据库。
我刚开始也是在这个地方遇到麻烦,设置bufferSize为10,刚开始怎么也观察不到日志插入数据库,后来才知道被缓存了。
不要在多处定义数据库连接字符串
因为我们已经在Web.config中定义了数据库连接字符串:
1.
<
connectionStrings
>
2.
<
clear
/>
3.
<
add
name
=
"Default"
connectionString
=
"Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=AppBox;Data Source=."
/>
4.
</
connectionStrings
>
因此如果在log4net.config中再定义数据库连接字符串,总觉得不爽。
经过在网上一番搜索,居然发现log4net v1.2.10不支持这个Asp.Net2.0的特性,不过给出了一个解决方法。
我们需要在AppBox中添加一个CS文件:
01.
using
System;
02.
using
System.Collections.Generic;
03.
using
System.Web;
04.
using
log4net;
05.
using
log4net.Appender;
06.
using
System.Configuration;
07.
namespace
AppBox
08.
{
09.
/// <summary>
10.
///
11.
/// An appender for Log4Net that uses a database based on the connection string name.
12.
/// </summary>
13.
public
class
Log4NetConnectionStringNameAdoNetAppender : AdoNetAppender
14.
{
15.
private
static
ILog _Log;
16.
/// <summary>
17.
/// Gets the log.
18.
/// </summary>
19.
/// <value>The log.</value>
20.
protected
static
ILog Log
21.
{
22.
get
23.
{
24.
if
(_Log ==
null
)
25.
_Log = LogManager.GetLogger(
typeof
(Log4NetConnectionStringNameAdoNetAppender));
26.
return
_Log;
27.
}
28.
}
29.
private
string
_ConnectionStringName;
30.
/// <summary>
31.
/// Initialize the appender based on the options set
32.
/// </summary>
33.
/// <remarks>
34.
/// <para>
35.
/// This is part of the <see cref="T:log4net.Core.IOptionHandler"/> delayed object
36.
/// activation scheme. The <see cref="M:log4net.Appender.AdoNetAppender.ActivateOptions"/> method must
37.
/// be called on this object after the configuration properties have
38.
/// been set. Until <see cref="M:log4net.Appender.AdoNetAppender.ActivateOptions"/> is called this
39.
/// object is in an undefined state and must not be used.
40.
/// </para>
41.
/// <para>
42.
/// If any of the configuration properties are modified then
43.
/// <see cref="M:log4net.Appender.AdoNetAppender.ActivateOptions"/> must be called again.
44.
/// </para>
45.
/// </remarks>
46.
public
override
void
ActivateOptions()
47.
{
48.
PopulateConnectionString();
49.
base
.ActivateOptions();
50.
}
51.
/// <summary>
52.
/// Populates the connection string.
53.
/// </summary>
54.
private
void
PopulateConnectionString()
55.
{
56.
// if connection string already defined, do nothing
57.
if
(!String.IsNullOrEmpty(ConnectionString))
return
;
58.
// if connection string name is not available, do nothing
59.
if
(String.IsNullOrEmpty(ConnectionStringName))
return
;
60.
// grab connection string settings
61.
ConnectionStringSettings settings = ConfigurationManager
62.
.ConnectionStrings[ConnectionStringName];
63.
// if connection string name was not found in settings
64.
if
(settings ==
null
)
65.
{
66.
// log error
67.
if
(Log.IsErrorEnabled)
68.
Log.ErrorFormat(
"Connection String Name not found in Configuration: {0}"
,
69.
ConnectionStringName);
70.
// do nothing more
71.
return
;
72.
}
73.
// retrieve connection string from the name
74.
ConnectionString = settings.ConnectionString;
75.
}
76.
/// <summary>
77.
/// Gets or sets the name of the connection string.
78.
/// </summary>
79.
/// <value>The name of the connection string.</value>
80.
public
string
ConnectionStringName
81.
{
82.
get
{
return
_ConnectionStringName; }
83.
set
{ _ConnectionStringName = value; }
84.
}
85.
}
86.
}
然后修改log4net.config文件:
1.
<
appender
name
=
"AdoNetAppender"
type
=
"AppBox.Log4NetConnectionStringNameAdoNetAppender"
>
2.
<
bufferSize
value
=
"0"
/>
3.
<
connectionType
value
=
"System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral,PublicKeyToken=b77a5c561934e089"
/>
4.
<
connectionStringName
value
=
"Default"
></
connectionStringName
>
5.
........
6.
........
7.
</
appender
>
注意,在log4net.config中我们指定使用名为 Default 的连接字符串。
使用log4net
调用方法倒很简单,比如在登录页面:
1.
private
static
readonly
log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
2.
protected
void
btnSubmit_Click(
object
sender, EventArgs e)
3.
{
4.
// ....
5.
logger.Info(String.Format(
"用户 - {0} - 登录成功"
, tbxUserName.Text));
6.
}
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn
文章转载自:博客园