log4net 写日志到 Sql server 数据库
本示例采用vs2010,原代码下载 /Files/zycblog/log4netDB.rar
1 新建数据库表MyLog ,脚本为
1 CREATE TABLE [dbo].[Log](
2 [Id] [int] IDENTITY(1,1) NOT NULL,
3 [Date] [datetime] NOT NULL,
4 [Thread] [varchar](255) NOT NULL,
5 [Level] [varchar](50) NOT NULL,
6 [Logger] [varchar](255) NOT NULL,
7 [Message] [varchar](4000) NOT NULL,
8 [Exception] [varchar](2000) NULL
9 ) ON [PRIMARY]
2 [Id] [int] IDENTITY(1,1) NOT NULL,
3 [Date] [datetime] NOT NULL,
4 [Thread] [varchar](255) NOT NULL,
5 [Level] [varchar](50) NOT NULL,
6 [Logger] [varchar](255) NOT NULL,
7 [Message] [varchar](4000) NOT NULL,
8 [Exception] [varchar](2000) NULL
9 ) ON [PRIMARY]
2 在项目AssemblyInfo.cs文件里新增一行
[assembly: log4net.Config.XmlConfiguratorAttribute(Watch = true)]
3 在项目config文件里新增如下节
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net debug="false">
<appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
<param name="File" value="c:\Log\DBLog.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>
<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
<bufferSize value="10"/>
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<connectionString value="server=192.168.1.100;database=ZYC;user id=sa;password=password"/>
<commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) 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="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>
<level value="DEBUG" />
<appender-ref ref="ADONetAppender" />
</root>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net debug="false">
<appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
<param name="File" value="c:\Log\DBLog.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>
<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
<bufferSize value="10"/>
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<connectionString value="server=192.168.1.100;database=ZYC;user id=sa;password=password"/>
<commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) 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="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>
<level value="DEBUG" />
<appender-ref ref="ADONetAppender" />
</root>
其中
<root>
<level value="DEBUG" />
<appender-ref ref="ADONetAppender" />
</root>
节的appender-ref 是ADONetAppender,对应上面的配置是连接到数据库,俱体内容看config文件就可以知道连接串,及对应数据库表信息参数
4 测试代码为
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(MyLog));
public static void LogInfo()
{
log.Info("log日¨?志?");
}
至此log信息就新增到数据库了
注意事项
引用log4net.dll
引用System.Data.dll