无法找到带.Net Core 1.0的NLog - LayoutRenderer

时间:2016-11-18 19:01:36

标签: asp.net-core nlog

这与this question非常相关,但对于.net Core。我有一个添加UserId的自定义布局渲染器,直到我尝试从" net461"升级在project.json中,#net; netcoreapp1.0"。当我这样做时,我不得不删除NLog.Extensions,因为它给了我这个错误:

Package NLog.Extensions 1.0.1 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package NLog.Extensions 1.0.1 supports: net45 (.NETFramework,Version=v4.5)

所有内容编译都很好,但在运行时,我在internal-nlog.txt日志文件中收到此错误,当然,它没有点击我的自定义布局渲染器。:

NLog.NLogConfigurationException: Error when setting property 'Layout' on NLog.Targets.DatabaseParameterInfo ---> System.ArgumentException: LayoutRenderer cannot be found: 'aspnet-user-id'

是否有NLog.Extensions的替代品,或者我错过了什么?

以下是来自project.json的引用:

"NLog.Extensions.Logging": "1.0.0-rtm-alpha4",
"NLog.Web.AspNetCore": "4.2.4"  },

我在Startup.cs和NLog.config中配置的所有内容与更改框架版本之前完全相同。如果发布这些内容有帮助,请告诉我,我会这样做。 我很感激帮助。

编辑:添加了project.json内容

"dependencies":{  
    "Microsoft.NETCore.App":{  
        "version":"1.0.1",
        "type":"platform"
    },
    "Microsoft.AspNetCore.Authentication.Cookies":"1.0.0-*",
    "Microsoft.AspNetCore.Diagnostics":"1.0.0-*",
    "Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore":"1.0.0-*",
    "Microsoft.AspNetCore.Identity.EntityFrameworkCore":"1.0.0-*",
    "Microsoft.AspNetCore.Mvc":"1.0.0-*",
    "Microsoft.AspNetCore.Server.IISIntegration":"1.0.0-*",
    "Microsoft.AspNetCore.Server.Kestrel":"1.1.0-preview1-final",
    "Microsoft.AspNetCore.StaticFiles":"1.0.0-*",
    "Microsoft.EntityFrameworkCore.SqlServer":"1.1.0-preview1-final",
    "Microsoft.EntityFrameworkCore.Tools":"1.0.0-*",
    "Microsoft.Extensions.Configuration":"1.1.0-preview1-final",
    "Microsoft.Extensions.Configuration.EnvironmentVariables":"1.1.0-preview1-final",
    "Microsoft.Extensions.Options.ConfigurationExtensions":"1.0.0-*",
    "Microsoft.Extensions.Configuration.Json":"1.0.0-*",
    "Microsoft.Extensions.Configuration.UserSecrets":"1.0.0-*",
    "Microsoft.Extensions.Logging":"1.1.0-preview1-final",
    "Microsoft.Extensions.Logging.Console":"1.0.0-*",
    "Microsoft.Extensions.Logging.Debug":"1.0.0-*",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader":"14.0.0-rc2-final",
    "Microsoft.VisualStudio.Web.CodeGenerators.Mvc":{  
        "version":"1.0.0-*",
        "type":"build"
    },
    "RestSharp.NetCore":"105.2.3",
    "Dapper":"1.50.2",
    "NLog.Extensions.Logging":"1.0.0-rtm-alpha4",
    "NLog.Web.AspNetCore":"4.2.4"
},
"tools":{  
    "Microsoft.AspNetCore.Razor.Tools":{  
        "version":"1.0.0-*",
        "imports":"portable-net45+win8+dnxcore50"
    },
    "Microsoft.AspNetCore.Server.IISIntegration.Tools":{  
        "version":"1.0.0-*",
        "imports":"portable-net45+win8+dnxcore50"
    },
    "Microsoft.EntityFrameworkCore.Tools":{  
        "version":"1.0.0-*",
        "imports":[  
            "portable-net45+win8+dnxcore50",
            "portable-net45+win8"
        ]
    },
    "Microsoft.Extensions.SecretManager.Tools":{  
        "version":"1.0.0-*",
        "imports":"portable-net45+win8+dnxcore50"
    }
},
"frameworks":{  
    "netcoreapp1.0":{  
        "imports":[  
            "dotnet5.6",
            "portable-net45+win8"
        ]
    }
}

3 个答案:

答案 0 :(得分:2)

假设您正在使用this NLog.Extensions package - 它不支持NETSTANDARD,因此您无法将其与netcoreapp1.0一起使用

升级ASP.NET(到ASP.NET Core)并非易事,似乎该库不再处于活动状态。

幸运的是NLog.Web.AspNetCore中有一个aspnet-user-identity,您已经包含了该内容。

所以用${aspnet-user-id}

替换${aspnet-user-identity}

答案 1 :(得分:0)

我不知道为什么,但当我注释掉电子邮件目标和记录器时,自定义LayoutRenderer再次开始工作。 作为参考,这是当前的nlog.config文件,其中已注释掉电子邮件。

<?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="Warn"
  internalLogFile="C:\temp\Logs\internal-nlog.txt">

<targets>
<target xsi:type="Null" name="blackhole" />
<target name="database" xsi:type="Database" >

  <connectionString>
    Server=localhost;Database=LogDatabase;Trusted_Connection=True;MultipleActiveResultSets=true
  </connectionString>

  <commandText>
    insert into dbo.Log (
      UserId, Application, LogDate, Level, Message, Exception
    ) values (
      @User, @Application, @Logged, @Level, @Message, @Exception
    );
  </commandText>

  <parameter name="@application" layout="WebApplicationNameHere" />
  <parameter name="@logged" layout="${date}" />
  <parameter name="@level" layout="${level}" />
  <parameter name="@message" layout="${message}" />
  <parameter name="@user" layout="${aspnet-user-id} " />  <!-- This is a custom attribute, set in NLogHelper.cs-->
  <parameter name="@exception" layout="${exception:tostring}" />
</target>

<!--<target name="email" xsi:type="Mail" 
        subject="System Error" 
        body="Date-Time:${newline}${longdate}${newline}${newline}Machine:${newline}${machinename}${newline}${newline}User:${newline}${aspnet-user-identity} ${newline}${newline}Message:${newline}${message}"
        to="first.last@domain.com" 
        from="support@mg.domain.com"
        Encoding="UTF-8"
        smtpUsername="postmaster@mg.domain.com"
        enableSsl="true"
        smtpPassword="passwordgoeshere"
        smtpAuthentication="Basic"
        smtpServer="smtp.mailgun.org"
        smtpPort="587" 
 /> -->
 </targets>
<rules>
<!--Skip Microsoft's verbose logging -->
<logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
<logger name="*" minlevel="Trace" writeTo="database" />
<!--<logger name="*" level="Error" writeTo="email"/>-->
</rules>  
<extensions>
   <!--enable NLog.Web for ASP.NET Core-->
  <add assembly="NLog.Web.AspNetCore"/>
</extensions>
</nlog>

如果我取消注释电子邮件部分,我会收到以下错误,但该电子邮件部分没有任何问题。我没有使用电子邮件部分尝试的唯一原因是因为我意识到我曾经在发生异常时收到电子邮件,现在我没有,并且正在尝试解决这个问题。

2016-11-22 12:42:13.7563 Warn Error has been raised. Exception: NLog.NLogConfigurationException: Error when setting property 'Layout' on NLog.Targets.DatabaseParameterInfo ---> System.ArgumentException: LayoutRenderer cannot be found: 'aspnet-user-id'
at NLog.Config.Factory`2.CreateInstance(String name)
at NLog.Layouts.LayoutParser.ParseLayoutRenderer(ConfigurationItemFactory configurationItemFactory, SimpleStringReader sr)
at NLog.Layouts.LayoutParser.CompileLayout(ConfigurationItemFactory configurationItemFactory, SimpleStringReader sr, Boolean isNested, String& text)
at NLog.Layouts.SimpleLayout.set_Text(String value)
at NLog.Internal.PropertyHelper.TryNLogSpecificConversion(Type propertyType, String value, Object& newValue, ConfigurationItemFactory configurationItemFactory)
at NLog.Internal.PropertyHelper.SetPropertyFromString(Object obj, String propertyName, String value, ConfigurationItemFactory configurationItemFactory)
--- End of inner exception stack trace ---
at NLog.Internal.PropertyHelper.SetPropertyFromString(Object obj, String propertyName, String value, ConfigurationItemFactory configurationItemFactory)
at NLog.Config.XmlLoggingConfiguration.ConfigureObjectFromAttributes(Object targetObject, NLogXmlElement element, Boolean ignoreType)
at NLog.Config.XmlLoggingConfiguration.AddArrayItemFromElement(Object o, NLogXmlElement element)
at NLog.Config.XmlLoggingConfiguration.SetPropertyFromElement(Object o, NLogXmlElement element)
at NLog.Config.XmlLoggingConfiguration.ParseTargetElement(Target target, NLogXmlElement targetElement)
at NLog.Config.XmlLoggingConfiguration.ParseTargetsElement(NLogXmlElement targetsElement)
at NLog.Config.XmlLoggingConfiguration.ParseNLogElement(NLogXmlElement nlogElement, String filePath, Boolean autoReloadDefault)
at NLog.Config.XmlLoggingConfiguration.Initialize(XmlReader reader, String fileName, Boolean ignoreErrors)

答案 2 :(得分:0)

在节点下的NLog.config文件中添加以下

<add assembly="NLog.Web.AspNetCore" />

希望这能解决你的问题。