Also use Log4net to log logging to the database configuration (i)
I've been trying to make a general-purpose logging system that makes it easy for different business group calls to log and analyze. Originally intended to write one, and later found that each business group may need different records, such as the use of database to record, TXT file form to record, of course, these are the most commonly used records. and different business groups want to have their own record data tables, and do not want to be mixed with the data of other business groups. So the architecture I envisioned earlier was burned, because it was not flexible enough and it was difficult and long-term. Just at this time think of the project before the time to use the Log4net Open Source log framework, and then new Google a bit, found that awesome. Today I would like to introduce you to log4net.
Description: Log4net is a feature-famous open source logging component. It allows logging of. NET applications to a variety of media, including: Files, consoles, window things logs, and databases (MS SQL Server, Access, Oracle9i, DB2, SQLite), And we can also set and mark the level of logs and so on.
For log4net I think everyone is no longer a stranger, so I ran directly into the topic, I am going to show how to configure the database and custom table fields (that is, custom properties). About the configuration of log4net, there are too many online content, about the database configuration on the Internet there are many. But I find a lot of it is a round-the-time, and there is no real solution to database configuration and custom table field problems, most people out of the solution is just put together, not used.
OK, using log4net in. NET to log logs to the database, start now:
(1), we first need a configuration file, I named it log4net.config, the content is as follows:
<?xml version= "1.0"?><configuration> <configSections> <section name= "log4net" type= "log4net". Config.log4netconfigurationsectionhandler,log4net "/> </configSections> <log4net> <root> < Level value= "INFO" ></level> <appender-ref ref= "Adonetappender_sql"/> </root> <logg Er name= "weblogger" > <level value= "INFO"/> <appender-ref ref= "Adonetappender"/> </LOGGER&G T <logger name= "WebTest" > <level value= "INFO"/> <appender-ref ref= "adonettest"/> </logger > <!--SQL database--<appender name= "Adonetappender" type= "log4net. Appender.adonetappender "> <buffersize value="/> <!--SQL data source, install SQL client locally--&L T;connectiontype value= "System.Data.SqlClient.SqlConnection, System.Data, version=1.0.3300.0, Culture=neutral, publickeytoken=b77a5c561934e089 "/> <!--SQL connection string-- <connectionstring value= "Data source=shuju-bailing\sqlexpress;initial catalog=logsys;integrated security=False; Persist Security info=true; User Id=sa; password=123456 "/> <commandtext value=" INSERT into Log ([recordtime],[levelname],[message],[exception],[ UserID]) VALUES (@log_date, @log_level, @message, @exception, @UserID) "/> <parameter> <parame Tername value= "@log_date"/> <dbtype value= "DateTime"/> <layout type= "log4net. Layout.rawtimestamplayout "/> </parameter> <parameter> <parametername value=" @log_level " /> <dbtype value= "String"/> <size value= "/> <layout type= log4net". Layout.patternlayout "> <conversionpattern value="%level "/> </layout> </parameter> ; <parameter> <parametername value= "@exception"/> <dbtype value= "String"/> <size V Alue= "/>" <layout type= "log4net. Layout.exceptionlayout "/> </parameter> <parameter> <parametername value=" @message "/> <dbtype value= "String"/> <size value= "4000"/> <layout type= "log4net. Layout.patternlayout "> <conversionpattern value="%message "/> </layout> </parameter& Gt <!--Custom Members-<parameter> <parametername value= "@UserID"/> <dbtype value= "Int32" /> <layout type= "JJ. Data.LogCommon.CustomLayout "> <conversionpattern value="%userid "/> </layout> </parameter> </appender> <!--SQL database--<appender name= "adonettest" type= "log4net. Appender.adonetappender "> <buffersize value="/> <!--SQL data source, install SQL client locally--<connection Type value= "System.Data.SqlClient.SqlConnection, System.Data, version=1.0.3300.0,Culture=neutral, publickeytoken=b77a5c561934e089 "/> <!--SQL connection string--<connectionstring value=" Data s Ource=shuju-bailing\sqlexpress;initial catalog=logsys;integrated security=false;persist Security info=True; User Id=sa; password=123456 "/> <commandtext value=" INSERT into Logtest ([recordtime],[levelname],[message],[exception],[u Serid]) VALUES (@log_date, @log_level, @message, @exception, @UserID) "/> <parameter> <parameternam E value= "@log_date"/> <dbtype value= "DateTime"/> <layout type= "log4net. Layout.rawtimestamplayout "/> </parameter> <parameter> <parametername value=" @log_level " /> <dbtype value= "String"/> <size value= "/> <layout type= log4net". Layout.patternlayout "> <conversionpattern value="%level "/> </layout> </parameter> ; <parameter> <parametername value= "@exception"/> <dbtype value= "String"/> <size value= "/> <layout type= log4net". Layout.exceptionlayout "/> </parameter> <parameter> <parametername value=" @message "/> <dbtype value= "String"/> <size value= "4000"/> <layout type= "log4net. Layout.patternlayout "> <conversionpattern value="%message "/> </layout> </parameter& Gt <!--Custom Members-<parameter> <parametername value= "@UserID"/> <dbtype value= "Int32" /> <layout type= "JJ. Data.LogCommon.CustomLayout "> <conversionpattern value="%userid "/> </layout> </par Ameter> </appender> <!--<root> <level value= "DEBUG"/> <appender-ref ref= "A Donetappender "/> </root>--> </log4net> <startup> <supportedruntime version=" v4 .0 "sku=". Netframework,version=v4.0 "/> </startup> </configuration>
This is a very complete log4net configuration file, the careful classmate will find, I configured two identical table, yes, this is for me to do the test, I want to try to see if he can according to different business needs to separate logs into different tables (the experiment proves that there is absolutely no problem).
<logger name= "Weblogger" > <level value= "INFO"/> <appender-ref ref= "Adonetappender"/> </logger><logger name= "WebTest" > <level value= "INFO"/> <appender-ref ref= "adonettest "/></logger>
The above configuration I would like to highlight, Logger configuration point tells us that the log information will be in the form of Adonetappender and adonettest respectively output, and these two forms are the two tables we configured above, you carefully observe the Appender configuration section The Name property, Haha, is not suddenly understood. In fact, if we really understand the log4net configuration instructions, in fact, the use of log4net is naturally mastered, the other is to look at the corresponding API documentation.
Well, today we mainly talk about the log4net log input to the database configuration file, we first understand, of course, the background or write code, I will be in the background code to organize and publish tomorrow. Because of the words are written out a bit dizzy, we look dizzy, so we slowly write.
Just write it here, I'm lark, I'll see you tomorrow.
Also use Log4net to log logging to the database configuration (i)