首页 > 代码库 > Log4Net
Log4Net
<?xml version="1.0" encoding="utf-8" ?><configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> </configSections> <log4net debug="true"> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <!--file可以指定具體的路徑 eg : d:\\test.log。不指定的話log被生成在專案的bin/Debug 或者 bin/Release目錄下 (web的專案 預設生成在根目錄下)--> <file value="logs\\chkfare\\error\\error.txt" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyyMMdd‘.log‘" /> <staticLogFileName value="false" /> <layout type="log4net.Layout.PatternLayout"> <!--指定log的格式--> <conversionPattern value="[%date] %thread -- %-5level -- %logger [%M] -- %message%newline" /> </layout> </appender> <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender"> <param name="bufferSize" value="0" /> <param name="connectionType" value="System.Data.SqlClient.SqlConnection" /> <param name="connectionString" value="Connection Timeout=1;data source=.;initial catalog=ErrorLogDB;integrated security=false;persist security info=True;User ID=sa;Password=password" /> <param name="commandText" value="INSERT INTO OTB_TKT_Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" /> <param name="parameter" type="log4net.Appender.AdoNetAppenderParameter"> <param name="parameterName" value="@log_date" /> <param name="dbType" value="DateTime" /> <param name="layout" type="log4net.Layout.RawTimeStampLayout" /> </param> <param name="parameter" type="log4net.Appender.AdoNetAppenderParameter"> <param name="parameterName" value="@thread" /> <param name="dbType" value="String" /> <param name="size" value="255" /> <param name="layout" type="log4net.Layout.PatternLayout"> <param name="conversionPattern" value="%thread" /> </param> </param> <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> <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender"> <authentication value="Basic" /> <to value="发送的目标邮箱 例如 to@qq.com" /> <from value="从哪里发出的邮箱 例如 from123456@qq.com" /> <username value="邮箱账号 例如 from123456" /> <password value="邮箱密码" /> <subject value="网站报错" /> <smtpHost value="smtp.qq.com" /> <bufferSize value="512" /> <lossy value="true" /> <evaluator type="log4net.Core.LevelEvaluator"> <threshold value="Error"/> </evaluator> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%n记录时间:%date 线程ID:[%thread] 日志级别:%-5level 出错类:%logger property:[%property{NDC}] - %n错误描述:%message%newline%n%exception% %n" /> </layout> </appender> <root> <level value="DEBUG" /> <!--指定將此級別及以上的log列印到log檔中--> <appender-ref ref="RollingLogFileAppender" /> <appender-ref ref="AdoNetAppender" /> <appender-ref ref="SmtpAppender" /> </root> </log4net></configuration>
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]
public static readonly ILog mo_Log = LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);//記錄異常
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
Mysql
<appender name="AdoNetAppenderForMySql" type="log4net.Appender.AdoNetAppender"> <param name="bufferSize" value="0" /> <param name="connectionType" value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data" /> <param name="connectionString" value="Connection Timeout=1;Server=localhost;Database=ErrorLogDB; User=root;Password=password;Use Procedure Bodies=false;Charset=utf8;Allow Zero Datetime=True; Pooling=false; Max Pool Size=50;" /> <param name="commandText" value="INSERT INTO Log (Date,Thread,Level,Logger,Message,Exception) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" /> <param name="parameter" type="log4net.Appender.AdoNetAppenderParameter"> <param name="parameterName" value="@log_date" /> <param name="dbType" value="DateTime" /> <param name="layout" type="log4net.Layout.RawTimeStampLayout" /> </param> <param name="parameter" type="log4net.Appender.AdoNetAppenderParameter"> <param name="parameterName" value="@thread" /> <param name="dbType" value="String" /> <param name="size" value="255" /> <param name="layout" type="log4net.Layout.PatternLayout"> <param name="conversionPattern" value="%thread" /> </param> </param> <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>
CREATE TABLE `log` ( `Id` int(11) NOT NULL AUTO_INCREMENT, `Date` datetime NOT NULL, `Thread` varchar(255) NOT NULL DEFAULT ‘‘, `Level` varchar(255) NOT NULL DEFAULT ‘‘, `Logger` varchar(255) NOT NULL DEFAULT ‘‘, `Message` varchar(4000) NOT NULL DEFAULT ‘‘, `Exception` varchar(4000) NOT NULL DEFAULT ‘‘, PRIMARY KEY (`Id`)) AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
Log4Net
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。