These two days encountered log4net write logs to MySQL database, sometimes in a project can, sometimes B project has problems, sometimes test environment is not a problem, to the formal deployment environment and problems, after two days of suffering, finally figured out the clue.
1. Configuration status
Config file log4net configuration Section :
<!--The following is log4net configuration--
<log4net>
<root>
<level value= "All"/>
<appender-ref ref= "Adonetappender"/>
</root>
<appender name= "Adonetappender" type= "log4net. Appender.adonetappender ">
<buffersize value= "1"/>
<connectiontype value= "MySql.Data.MySqlClient.MySqlConnection, Mysql.data, version=6.8.5.0, Culture=neutral, publickeytoken=c5687fc88969c44d "/>
<connectionstring value= "server=192.168.10;2database=userlogs; uid=****; pwd=****; "/>
<commandtext value= "INSERT into logs (log_datetime,log_thread,log_level,log_logger,log_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= "/>"
<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= "/>"
<layout type= "log4net. Layout.exceptionlayout "/>
</parameter>
</appender>
</log4net>
The version number of the Mysql.data referenced by the project is: 5.1.5.0
Native installed: C:\Program Files (x86) \mysql\mysql Connector Net 6.8.3
You crossing, in this unbearable environment, call log4net write log to MySQL, not success is a matter of course!!!
2. Cause Analysis:
After the project starts, according to the MySql.Data.dll version number specified in the configuration section ConnectionType, look for the 6.8.5.0 MySql.Data.dll in the bin directory, and find that the bin is not there, go to the MySQL installation drive where c \ Program Files (x86) \mysql\mysql Connector Net 6.8.3 were not found.
The version of MySql.Data.dll could not be loaded at the end, so it was unsuccessful when the write log was called. How can I modify the log to write successfully?
3. Workaround:
Method One:
Modified <connectiontype value= "MySql.Data.MySqlClient.MySqlConnection, Mysql.data, version=6.8.5.0, culture= Neutral, publickeytoken=c5687fc88969c44d "/>
For: <connectiontype value= "MySql.Data.MySqlClient.MySqlConnection, Mysql.data, version=5.1.5.0, Culture=neutral, Publickeytoken=c5687fc88969c44d "/>
Then, when loading MySql.Data.dll, the system first looks for the MySql.Data.dll with the version number 5.1.5.0 under the bin. The load succeeds, so it can be written successfully.
Method Two:
Modified <connectiontype value= "MySql.Data.MySqlClient.MySqlConnection, Mysql.data, version=6.8.5.0, Culture=neutral, Publickeytoken=c5687fc88969c44d "/>
For: <connectiontype value= "MySql.Data.MySqlClient.MySqlConnection, Mysql.data, version=6.8.3.0, Culture=neutral, Publickeytoken=c5687fc88969c44d "/>
Then, the system when loading MySql.Data.dll, first in the bin to look for version number 6.8.3.0 MySql.Data.dll, did not find, then go to the System installation directory to find, the results found, loaded successfully, it can be successfully written.
Method Three:
Locate the MySql.Data.dll version 6.8.5.0, and then the project directly references the MySql.Data.dll with the version number 6.8.5.0. can also be successful.
In combination with the above three methods, the most recommended is the version of the DLL referenced by the project and the config file log4net the ConnectionType section specifies the same versions, even if the deployment environment changes, log4net log to the MySQL database can also work properly.
Also note:
The version of the project is best consistent with the Mysql.data.dll runtime version, both based on 2.0,3.5 or both 4.0 and 4.5.
Log4net refers to Mysql.Data.dll, but it is not possible to write to the MySQL database solution