- 首先网站引用log4Net.dll
- 在网站根目录下创建Log4Net.config文件,Log4Net.config的内容如下
View Code<?xml version="1.0" encoding="utf-8"?>
<log4net>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<!--BufferSize为缓冲区大小,只有日志记录超1条才会一块写入到数据库-->
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<!--连接数据库字符串-->
<connectionString value="Data Source=WANGSHAN;Initial Catalog=UZDB;Persist Security Info=True;User ID=sa;Password=jinshanshan" />
<!--插入到表Log-->
<commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
<!--日志记录时间,RawTimeStampLayout为默认的时间输出格式 -->
<parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread" />
</layout>
</parameter>
<parameter>
<parameterName value="@log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value="@logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="@exception" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
<root>
<!-- Set this to "ERROR" before release -->
<!--Highest: OFF->FATAL->RROR->WARN->INFO->DEBUG->ALL :Lowest -->
<level value="ALL" />
<appender-ref ref="AdoNetAppender" />
<!-- <appender-ref ref="SmtpAppender" /> -->
<!--<appender-ref ref="FileAppender" /> -->
</root>
</log4net> - 在网站properties文件夹下的AssemblyInfo.cs中,加入代码:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)],注意,如果是在类库中使用log4Net,需在类库的AssemblyInfo.cs文件中添加以上代码即可。
-
创建Log4Net.config文件中定义的数据库,数据库脚本如下:
View Code/****** Object: Table [dbo].[Log] Script Date: 03/12/2012 16:06:24 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Log](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Date] [datetime] NOT NULL,
[Thread] [varchar](255) NOT NULL,
[Level] [varchar](50) NOT NULL,
[Logger] [varchar](255) NOT NULL,
[Message] [varchar](4000) NOT NULL,
[Exception] [varchar](2000) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO -
页面cs或类库中的使用。首先声明记录Log的变量ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);使用方法如:logger.Error("message");//记录错误日志;logger.Info("message");//记录日志信息
通过以上步骤操作,就可以将Log4Net之前记录到数据库了