.net Core 2.0的NLog数据库目标

时间:2018-01-10 06:24:50

标签: c# nlog asp.net-core-2.0

我正在尝试使用.net Core 2.0中的NLog将各种日志写入数据库和文件。问题是,将日志写入文件工作完全正常。但是在将日志写入数据库时​​总是会出现以下错误:

  

2018-01-10 08:48:29.5666错误初始化目标'数据库目标[db]'时出错。异常:System.TypeLoadException:无法加载类型' System.Data.SqlClient'来自汇编&n; 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)           在NLog.Targets.DatabaseTarget.SetConnectionType()           在NLog.Targets.DatabaseTarget.InitializeTarget()           在NLog.Targets.Target.Initialize(LoggingConfiguration配置)

但我的项目中有 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先生想要其他东西,我无法找到答案。

1 个答案:

答案 0 :(得分:6)

这是因为我为我的数据库目标写了Uncaught Error: Template parse errors: 'ckeditor' is not a known element: 1. If 'ckeditor' is an Angular component, then verify that it is part of this module. 2. To allow any element add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("N' | translate:lang }}</span> <div formControlName="description" [froalaEditor] ></div> [ERROR ->]<ckeditor></ckeditor> </mat-card-content> </mat-card> 。看起来有人应该更新文档。

顺便说一下,我不知道为什么删除dbProvider对我有用。

工作代码如下:

<dbProvider>System.Data.SqlClient</dbProvider>