mvc中使用log4net
新建一个log4net.config配置文件
View Code
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/> </configSections> <log4net> <!--定义输出到文件中--> <appender name="LogFileAppender" type="log4net.Appender.FileAppender"> <!--定义文件存放位置--> <file value="D:/log4netfile.txt" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyyMMdd-HH:mm:ss" /> <layout type="log4net.Layout.PatternLayout"> <!--每条日志末尾的文字说明--> <footer value="" /> <!--输出格式--> <!--样例:2008-03-26 13:42:32,111 [10] INFO Log4NetDemo.MainClass [(null)] - info--> <conversionPattern value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level 出错类:%logger property:[%property{NDC}] - 错误描述:%message%newline" /> </layout> </appender> <!--定义输出到控制台命令行中--> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <!--定义输出到windows事件中--> <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </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=127.0.0.1;database=ADMS;user id=sa;password=ymj126"/> <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="All" /> <!--文件形式记录日志--> <appender-ref ref="LogFileAppender" /> <!--控制台控制显示日志--> <!--<appender-ref ref="ConsoleAppender" />--> <!--Windows事件日志--> <!--<appender-ref ref="EventLogAppender" />--> <!-- 如果不启用相应的日志记录,可以通过这种方式注释掉 <appender-ref ref="AdoNetAppender_Access" /> --> <appender-ref ref="ADONetAppender" /> </root> <!--过滤Nhibernate日志--> <logger name="NHibernate" additivity="false"> <level value="OFF" /> <appender-ref ref="LogFileAppender" /> <appender-ref ref="ADONetAppender" /> </logger> <logger name="NHibernate.SQL" additivity="false"> <level value="OFF" /> <appender-ref ref="LogFileAppender" /> <appender-ref ref="ADONetAppender" /> </logger> </log4net> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> </system.web> </configuration>
注意:因为我用Nhibernate框架所以加上NHibernate和NHibernate.SQL两个logger节点,目的是为了过滤Nhibernate的日志信息
在Application_Start()加
//初始化log4net log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(Server.MapPath("~/log4net.config")));
自己写的类库方便调用
View Code
调用
Log4netHelper.Error(ex);