<? xml version = "1.0" encoding = "utf-8" ?> < configuration > <!--首先加入configSections配置节点--> < configSections > < section name = "log4net" type = "log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> </ configSections > < log4net >< br > < root > <!--(高) OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL (低) --> <!--限制日志记录级别--> < level value = "all" /> <!--将日志输出到文件--> < appender-ref ref = "FileAppender" /> <!--将日志输出到应用程序控制台--> < appender-ref ref = "ConsoleAppender" /> <!--将日志写到邮件中--> <!--<appender-ref ref="LogSmtpAppender"/>--> <!--将日志记录到数据库中。可以采用SQL和存储过程两种方式--> < appender-ref ref = "AdoNetAppender" /> <!--可以同时设置多个输出对象,没用到可以注释--> <!--将日志写到local syslog service (仅用于UNIX环境下)。--> <!--<appender-ref ref="LocalSyslogAppender"/>--> </ root >< br > <!--发邮件--> < appender name = "LogSmtpAppender" type = "log4net.Appender.SmtpAppender" > < authentication value = "Basic" /> <!--接受邮件的邮箱--> < to value = "7778888@qq.com" /> <!--发送邮件的邮箱--> < from value = "123456@126.com" /> <!--发送邮件的邮箱--> < username value = "123456@126.com" /> <!--发送邮件的邮箱密码--> < password value = "123456" /> <!--标题--> < subject value = "XXX应用错误日志消息" /> <!--smtp.qq.com--> < smtpHost value = "smtp.126.com" /> < bufferSize value = "2048" /> <!--超长部分是否丢弃--> < lossy value = "true" /> <!--日志级别 大于 WARN 的才发邮件--> < evaluator type = "log4net.Core.LevelEvaluator" > < threshold value = "ERROR" /> </ evaluator > < layout type = "log4net.Layout.PatternLayout" > < conversionPattern value = " %level :: %message %newlineLogger: %logger%newline Date: %date%newline" /> </ layout > </ appender >< br > <!-- 控制台日志 --> < appender name = "ConsoleAppender" type = "log4net.Appender.ConsoleAppender" > < layout type = "log4net.Layout.PatternLayout" > < conversionPattern value = "%n%d{yyyy-MM-dd HH:mm:ss} %m%n" /> </ layout > <!--指定范围内的等级才可以被记录--> <!--<filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="Info" /> <param name="LevelMax" value="Fatal" /> </filter>--> </ appender > <!--指定日记记录方式,以滚动文件的方式(文件记录)--> < appender name = "FileAppender" type = "log4net.Appender.RollingFileAppender" > <!--日志路径--> < param name = "File" value = "log/" /> <!--是否是向文件中追加日志--> < param name = "AppendToFile" value = "true" /> <!--log保留天数--> < param name = "MaxSizeRollBackups" value = "10" /> <!--每个文件最大1M--> < param name = "maximumFileSize" value = "1MB" /> <!--日志文件名是否是固定不变的--> < param name = "StaticLogFileName" value = "false" /> <!--日志文件名格式为:2008-08-31.log--> < param name = "DatePattern" value = "yyyy-MM-dd" .log""/> <!--日志根据日期滚动--> < param name = "RollingStyle" value = "Date" /> <!--布局--> < layout type = "log4net.Layout.PatternLayout" > < param name = "ConversionPattern" value = "%n记录时间:%d{yyyy-MM-dd HH:mm:ss} 线程名:[%t] 级别:%p 类名:%c 信息:%m%n" /> < param name = "Header" value = "----------------------------------------------------------- " /> </ layout > </ appender >< br > <!--数据库日记--> < appender name = "AdoNetAppender" type = "log4net.Appender.AdoNetAppender" > <!--设置为1方便查看结果,处于性能考虑这里的值应该略大一点,官方默认值是100,BufferSize为缓冲区大小,只有日志记录超设定值才会一块写入到数据库--> < bufferSize value = "1" /> <!--sqlserver 引用--> < connectionType value = "System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <!--连接数据库字符串--> < connectionString value = "data source=.;initial catalog=test;integrated security=false;persist security info=True;User ID=sa;Password=sqlserver;" /> <!--插入到表Log--> < commandText value = "INSERT INTO LogTest ([Date],[Level],[Logger],[Message],[Exception],[Description],[ObjType]) VALUES (@log_date,@log_level, @logger, @message, @exception,@Description,@ObjType)" /> <!--所有参数--> < parameter > < parameterName value = "@log_date" /> < dbType value = "DateTime" /> < layout type = "log4net.Layout.RawTimeStampLayout" /> </ 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 > <!--自定义输入字段--> < parameter > < parameterName value = "@Description" /> < dbType value = "String" /> < size value = "50" /> < layout type = "log4_Test.Log4netExpand.ExpandLayout" > < conversionPattern value = "%property{Description}" /> </ layout > </ parameter > < parameter > < parameterName value = "@ObjType" /> < dbType value = "String" /> < size value = "50" /> < layout type = "log4_Test.Log4netExpand.ExpandLayout" > < conversionPattern value = "%property{ObjType}" /> </ layout > </ parameter > </ appender > </ log4net > </ configuration > |