2018-01-10 08:48:29.5666错误 Error initializing target 'Database Target[db]'. Exception: System.TypeLoadException: Could not load type 'System.Data.SqlClient' from assembly 'NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c'. at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type, ObjectHandleOnStack keepalive) at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName) at System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark) at System.Type.GetType(String typeName, Boolean throwOnError) at NLog.Targets.DatabaseTarget.SetConnectionType() at NLog.Targets.DatabaseTarget.InitializeTarget() at NLog.Targets.Target.Initialize(LoggingConfiguration configuration)
但是我在项目中有 System.Data.SqlClient 如下:
<ItemGroup>
<PackageReference Include="EPPlus.Core" Version="1.5.4" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.2.0-beta3" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Session" Version="2.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.1" PrivateAssets="All" />
<PackageReference Include="NLog" Version="4.5.0-rc03" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.5.0-rc2" />
<PackageReference Include="System.ComponentModel" Version="4.3.0" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0-preview1-25914-04" />
<PackageReference Include="System.Data.SqlClient" Version="4.4.2" />
</ItemGroup>
我的NLOG代码:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" internalLogLevel="info" internalLogFile="C:\*****\Log2\asdaf.log">
<targets>
<target
xsi:type="File"
name="allfile"
fileName="C:\*****\Logs\${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId.Id}|${uppercase:${level}}|${logger}|${message} ${exception}" >
</target>
<target xsi:type="Database" name="db">
<commandText>INSERT INTO [LogEntries](TimeStamp, Message, Level, Logger) VALUES(getutcdate(), @msg, @level, @logger)</commandText>
<parameter name="@msg" layout="${message}" />
<parameter name="@level" layout="${level}" />
<parameter name="@logger" layout="${logger}" />
<dbProvider>System.Data.SqlClient</dbProvider>
<connectionString>*****</connectionString>
</target>
<target xsi:type="Null" name="blackhole" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="allOutputs" />
<logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
</rules>
</nlog>
我在Github上搜索了这个问题,但是没有一种解决方案适用于我。从Nuget获取System.Data.SqlClient应该足够了,但不幸的是,Nlog.config需要其他东西,我找不出来是什么。
System.Data.SqlClient
。 - KaraKaplanKhanSystem.Data.SqlClient.dll
。在我看来,项目已经有了它,但NLog找不到它。 - KaraKaplanKhan<dbProvider>
的值不太对。NLog似乎试图将其作为自己程序集的资源查找(如错误消息所述),而它可以在“调用”程序集中找到它作为资源。也许文档可以帮助解决问题? - FildorSystem.Data.SqlClient
,无论我搜索到哪里,所有人都像<dbProvider>System.Data.SqlClient</dbProvider>
或provider="System.Data.SqlClient"
这样写提供程序。 - KaraKaplanKhan