Log4net write DB2 memo via OLEDB & ODBC, log4netdb2
The project needs to record operation logs. Because it is a plug-in system, you do not need to consider the constraints of the company's framework. You can directly connect to the database in a two-layer architecture, and use log4net to connect to DB2. Forgive me for being a non-soft engineer. I have been learning about log4net before, but I have never had a chance to get in touch with my own framework record at work. This time, it's just this opportunity to test your skills.
Here, we will briefly introduce this plug-in system. Very simple. NET + DB2 configuration, two-layer architecture, the general picture is available, you need to switch to the permission control and log system. The work went smoothly. The permission system was so boring that log4net was used for logs, which was a big deal for Baidu. Refer to the ODBC configuration of MySql from Baidu to copy it here. This is a good operation. It's dumpfounded. No error is reported, and no self-built tables are written. This is a headache.
In the spirit of research, Baidu directly arranged and combined various keywords, such as log4net + DB2 + ODBC. Unfortunately, there were almost no keywords for log4net + DB2 + ODBC on Baidu, all of them are from SQL Server and Oracle cloud. Is it true that everyone does not need to use log4net to connect to DB2? The only blog related to this article is that DB2 V9.7 does not support log4net via ODBC and cannot be connected at all: (, okay ~~~~ "Cannot connect at all !!!" (My results later prove that he is wrong ). Then tom asked CSDN's Forum for help-No response; he wanted to go to stackoverflow to ask foreigners, but for a long time, google's JS library was broken down and asked no questions-speechless.
In this way, Xiao Bai had a bad mood for tonight. While cursing log4net's failure to connect to the database, Tom found a resume of ConfigurationMessages during breakpoint debugging. It said, "System cannot be found. data. dll "(this is basically the meaning, and Tom will not map it here ). With a flash of light, the System. Data. dll in the reference will be copied locally to set to true, run it, and become!
Here, Xiao Bai lists the configuration of connecting log4net to DB2 through ODBC in OLEDB:
Log4net via ODBC
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <configuration> 3 <configSections> 4 <section name = "log4net" type = "log4net. config. log4NetConfigurationSectionHandler, log4net "/> 5 </configSections> 6 7 <log4net debug =" true "> 8 <root> 9 <level value =" INFO "> </level> 10 <! -- File record format --> 11 <! -- <Appender-ref = "rollingFile"/> --> 12 <appender-ref = "AdoNetAppender_DB2"/> 13 </root> 14 15 <appender name = "AdoNetAppender_DB2" type = "log4net. appender. adoNetAppender, log4net "> 16 <bufferSize value =" 1 "/> 17 18 <! -- SQL Data source --> 19 <connectionType value = "System. Data. Odbc. OdbcConnection, System. Data"/> 20 21 <! -- SQL connection string --> 22 <connectionString value = "Driver = {IBM DB2 ODBC DRIVER}; Hostname = 192.168.44.11; Port = 50001; Database = xxx; Uid = xxx; pwd = xxx; Protocol = TCPIP; "/> 23 <commandText value =" insert into xxx VALUES ('','','',?, '','') "/> 24 25 <parameter> 26 <parameterName value =" @ log_level "/> 27 <dbType value =" String "/> 28 <size value =" 50 "/> 29 <layout type = "log4net. layout. patternLayout "value =" % level "/> 30 </parameter> 31 </appender> 32 </log4net> 33 </configuration>
Log4net via OLEDB
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <configuration> 3 <configSections> 4 <section name = "log4net" type = "log4net. config. log4NetConfigurationSectionHandler, log4net "/> 5 </configSections> 6 7 <log4net debug =" true "> 8 <root> 9 <level value =" INFO "> </level> 10 <! -- File record format --> 11 <! -- <Appender-ref = "rollingFile"/> --> 12 <appender-ref = "AdoNetAppender_DB2"/> 13 </root> 14 <appender name = "AdoNetAppender_DB2" type = "log4net. appender. adoNetAppender, log4net "> 15 <bufferSize value =" 1 "/> 16 <connectionType value =" System. data. oleDb. oleDbConnection, System. data "/> 17 <connectionString value =" Provider = IBMDADB2; Hostname = 192.168.44.11; Port = 50001; UID = xxxx; PWD = xxx; Database = xxx; Protocol = TC PIP; "/> 18 <commandText value =" insert into xxx VALUES ('','','',?, '','') "/> 19 <parameter> 20 <parameterName value =" @ message "/> 21 <dbType value =" String "/> 22 <size value =" 4000 "/> 23 <layout type = "log4net. layout. patternLayout "> 24 <conversionPattern value =" % message "/> 25 </layout> 26 </parameter> 27 </appender> 28 </log4net> 29 </configuration>
Finally, paste a breakpoint view on how to check the log4net error. Then you can find the error:
Log4net succeeded in SQL server, but cannot be written to access?
Open Directory Permissions
Log4net cannot output logs
Create a dll, first write a class, reference log4Net. dll
C # code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Public class Logger
{
Public static bool GetTracerSwitchor ()
{
String configPath = Application. StartupPath + "\ eSerialPro.exe ";
System. Configuration. Configuration config = System. Configuration. ConfigurationManager. OpenExeConfiguration (configPath );
Bool IsTracer = Convert. ToBoolean (config. etettings. Settings ["IsTracer"]. Value );
Return IsTracer;
}
/// <Summary>
/// Debugging information
/// </Summary>
/// <Param name = "msg"> </param>
Public static void Debug (string msg)
{
Bool IsTracer = GetTracerSwitchor ();
If (IsTracer)
{
Log4net. ILog log = log4net. LogManager. GetLogger ("Test ");
If (log. IsDebugEnabled)
{
Log. Debug (msg );
}
Log = null;
}
}
}
This call
Add information in Assembly.
[Assembly: log4net. Config. XmlConfigurator (ConfigFile = "log4net. config", Watch = true)]
Write a file named log4net. appconfig under the root directory of the executable program. The content is as follows:
C # code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<? Xml version = "1.0" encoding = "UTF-8"?>
<Confi ...... remaining full text>