如何为 mySQL 配置 log4net?
How to configure log4net for mySQL?
我正在尝试使用 log4net appender 在 mySQL 数据库中记录事件,但我没有设法正确配置 log4net 文件,
这里是 log4net.config:
<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
<bufferSize value="1"/>
<lossy value="false"/>
<connectionType value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>
<!--connectionString value="Server=my_server_ip_or_name;Database=my_db_name; Uid=my_user_name;Pwd=my_passwd;"/-->
<connectionString value="server=my_server_ip_or_name;database=my_db_name; user=my_user_name;password=my_passwd;port=3306"/>
<commandText value="INSERT INTO Log (dtLog,lbThread,lbLevel,lbLogger,lbMessage,lbException,lbContext) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, @context)"/>
<parameter>
<parameterName value="@log_date"/>
<dbType value="DateTime"/>
<layout type="log4net.Layout.RawTimeStampLayout"/>
</parameter>
<parameter>
<parameterName value="@thread"/>
<dbType value="String"/>
<size value="32"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%t"/>
</layout>
</parameter>
<parameter>
<parameterName value="@log_level"/>
<dbType value="String"/>
<size value="10"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%p"/>
</layout>
</parameter>
<parameter>
<parameterName value="@context"/>
<dbType value="String"/>
<size value="10"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%x"/>
</layout>
</parameter>
<parameter>
<parameterName value="@logger"/>
<dbType value="String"/>
<size value="512"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%c"/>
</layout>
</parameter>
<parameter>
<parameterName value="@message"/>
<dbType value="String"/>
<size value="4000"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%m"/>
</layout>
</parameter>
<parameter>
<parameterName value="@exception"/>
<dbType value="String"/>
<size value="4000"/>
<layout type="log4net.Layout.ExceptionLayout"/>
</parameter>
</appender>
在运行时,我得到以下异常:
{"Object reference not set to an instance of an object. (C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config line 295)"}
对应于以下配置行:
<add name="MySqlSiteMapProvider" type="MySql.Web.SiteMap.MySqlSiteMapProvider, MySql.Web, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="LocalMySqlServer" applicationName="/" />
log4net调试报如下错误:
iisexpress.exe Error: 0 : Authentication to host 'my_server_ip_or_name' for user 'my_user_name' using method 'mysql_native_password' failed with message: Access denied for user 'my_user_name'@'my_local_machine_name' (using password: YES)
log4net:ERROR [AdoNetAppender] ErrorCode: GenericFailure. Could not open database connection [server=my_server_ip_or_name;database=my_db_name; user=my_user_name;password=my_passwd;port=3306]. Connection string context [ConnectionString].
MySql.Data.MySqlClient.MySqlException (0x80004005): Authentication to host 'my_server_ip_or_name' for user 'my_usr_name' using method 'mysql_native_password' failed with message: Access denied for user 'my_user_name'@'my_local_machine_name' (using password: YES) ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Access denied for user 'my_user_name'@'my_local_machine_name' (using password: YES)
我不明白为什么 log4net 尝试连接到我的本地计算机而不是我配置的服务器。
知道我的配置有什么问题吗?
正如@TZHX 所注意到的,我必须在远程 IP 上为 mySQL 用户授予远程访问权限:
GRANT ALL ON my_db_name.* TO my_user_name@'my_remote_ip' IDENTIFIED BY 'my_passwd';
我还必须在 machine.config 文件中评论 MySql 站点地图配置:
<!--add name="MySqlSiteMapProvider" type="MySql.Web.SiteMap.MySqlSiteMapProvider, MySql.Web, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="LocalMySqlServer" applicationName="/" /-->
我正在尝试使用 log4net appender 在 mySQL 数据库中记录事件,但我没有设法正确配置 log4net 文件,
这里是 log4net.config:
<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
<bufferSize value="1"/>
<lossy value="false"/>
<connectionType value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>
<!--connectionString value="Server=my_server_ip_or_name;Database=my_db_name; Uid=my_user_name;Pwd=my_passwd;"/-->
<connectionString value="server=my_server_ip_or_name;database=my_db_name; user=my_user_name;password=my_passwd;port=3306"/>
<commandText value="INSERT INTO Log (dtLog,lbThread,lbLevel,lbLogger,lbMessage,lbException,lbContext) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, @context)"/>
<parameter>
<parameterName value="@log_date"/>
<dbType value="DateTime"/>
<layout type="log4net.Layout.RawTimeStampLayout"/>
</parameter>
<parameter>
<parameterName value="@thread"/>
<dbType value="String"/>
<size value="32"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%t"/>
</layout>
</parameter>
<parameter>
<parameterName value="@log_level"/>
<dbType value="String"/>
<size value="10"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%p"/>
</layout>
</parameter>
<parameter>
<parameterName value="@context"/>
<dbType value="String"/>
<size value="10"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%x"/>
</layout>
</parameter>
<parameter>
<parameterName value="@logger"/>
<dbType value="String"/>
<size value="512"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%c"/>
</layout>
</parameter>
<parameter>
<parameterName value="@message"/>
<dbType value="String"/>
<size value="4000"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%m"/>
</layout>
</parameter>
<parameter>
<parameterName value="@exception"/>
<dbType value="String"/>
<size value="4000"/>
<layout type="log4net.Layout.ExceptionLayout"/>
</parameter>
</appender>
在运行时,我得到以下异常:
{"Object reference not set to an instance of an object. (C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config line 295)"}
对应于以下配置行:
<add name="MySqlSiteMapProvider" type="MySql.Web.SiteMap.MySqlSiteMapProvider, MySql.Web, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="LocalMySqlServer" applicationName="/" />
log4net调试报如下错误:
iisexpress.exe Error: 0 : Authentication to host 'my_server_ip_or_name' for user 'my_user_name' using method 'mysql_native_password' failed with message: Access denied for user 'my_user_name'@'my_local_machine_name' (using password: YES)
log4net:ERROR [AdoNetAppender] ErrorCode: GenericFailure. Could not open database connection [server=my_server_ip_or_name;database=my_db_name; user=my_user_name;password=my_passwd;port=3306]. Connection string context [ConnectionString]. MySql.Data.MySqlClient.MySqlException (0x80004005): Authentication to host 'my_server_ip_or_name' for user 'my_usr_name' using method 'mysql_native_password' failed with message: Access denied for user 'my_user_name'@'my_local_machine_name' (using password: YES) ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Access denied for user 'my_user_name'@'my_local_machine_name' (using password: YES)
我不明白为什么 log4net 尝试连接到我的本地计算机而不是我配置的服务器。
知道我的配置有什么问题吗?
正如@TZHX 所注意到的,我必须在远程 IP 上为 mySQL 用户授予远程访问权限:
GRANT ALL ON my_db_name.* TO my_user_name@'my_remote_ip' IDENTIFIED BY 'my_passwd';
我还必须在 machine.config 文件中评论 MySql 站点地图配置:
<!--add name="MySqlSiteMapProvider" type="MySql.Web.SiteMap.MySqlSiteMapProvider, MySql.Web, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="LocalMySqlServer" applicationName="/" /-->