使用Log4net把日志写入到SqlServer数据库
1、官网URL: http://logging.apache.org/log4net/
2、配置文件参照URL: http://logging.apache.org/log4net/release/config-examples.html
3、在Web.config中加入以下代码
3.1:在configSections中加入
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
<configSections> <!--Log4net的配置--> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> </configSections>
3.2:log4net节点配置如下
<!--Log4net的配置--> <log4net> <!--本地Text文件--> <appender name="LogFileAppender" type="log4net.Appender.FileAppender" > <param name="File" value="c:\Log4\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> <!--SQL数据库--> <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender"> <bufferSize value="1" /> <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <connectionString value="data source=[database server];initial catalog=[database name];integrated security=false;persist security info=True;User ID=[user];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> <!-- 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>
4、在AssemblyInfo.cs中加入以下两行代码
using log4net;
[assembly: log4net.Config.XmlConfiguratorAttribute(Watch = true)]
5、代码调用测试如下:
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(HomeController));
public ActionResult Index()
{
log.Info("自定义Info级别的信息");
log.Error("error", new Exception(" error 信息 。。。"));
log.Fatal("fatal", new Exception(" fatal 信息 。。。"));
log.Info("info", new Exception(" info 信息 。。。"));
log.Debug("debug", new Exception(" debug 信息 。。。"));
log.Warn("warn", new Exception(" warn 信息 。。。"));
return View();
}
6、效果如下:
7、建表SQL语句:
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 )
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。