簡體   English   中英

MySqlConnector 和 NLog 的異常

[英]Exception with MySqlConnector and NLog

我正在嘗試使用 dotnet core 3.0 將 @[Bradley Grainger] 的優秀 MySqlConnector 連接到 Nlog 記錄器 - 需要 MySqlConnector 的異步功能。

使用 MySql.Data 運行良好,但使用 MySqlConnector 的性能要好得多。 web 應用程序在沒有附加 NLog 記錄器的情況下運行良好,但是當我按照說明嘗試使用記錄器時出現異常。

我已經創建了一個基本的 web api,(來自 VS 模板),除了嘗試附加到記錄器然后打開連接之外什么也不做。

包:

  • MySqlConnector 0.60.2
  • MySqlConnector.Logging.NLog 0.58.0
  • NLog.Web.AspNetCore 4.9.0

nlog.config 包含:

    <target name="database" xsi:type="Database" >
      <connectionString>${gdc:item=connectionString}</connectionString>
      <commandText>
        insert into log (
...

Program.cs 獲取連接字符串並將其設置為 NLog:

DotEnv.Config();
string connectionString = "server=" + Environment.GetEnvironmentVariable("SQL_HOST") ...
GlobalDiagnosticsContext.Set("connectionString", connectionString);

當我添加MySqlConnectorLogManager.Provider = new NLogLoggerProvider(); 根據自述文件到 Startup.cs 並進入 .Open() 方法

var connection = new MySqlConnection(connectionString);
connection.Open();

拋出以下異常

System.TypeInitializationException: 'The type initializer for 'MySql.Data.MySqlClient.MySqlConnection' threw an exception.'

Inner Exception:
FileNotFoundException: Could not load file or assembly 'System.Data.SqlClient, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

  StackTrace:
   at MySql.Data.MySqlClient.MySqlConnection.SetState(ConnectionState newState) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs:line 668
   at MySql.Data.MySqlClient.MySqlConnection.<OpenAsync>d__26.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs:line 292
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at MySql.Data.MySqlClient.MySqlConnection.Open() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs:line 280
   at WebApplication1.DataReaderHelper.ExecuteStoredProcedure(String procedureName)

請問有什么想法嗎?

您需要為數據庫目標設置<dbProvider>

例如

 <target name="database" xsi:type="Database" 
      dbProvider="MySql.Data.MySqlClient.MySqlConnection, MySql.Data" >

NLog Wiki 上有更多示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM