2016-11-18 80 views
2

这是非常相关的this question,但对于.NET的核心。我有一个用户ID添加自定义布局渲染器和它的工作,直到我在project.json为“netcoreapp1.0”试图从“net461”升级。当我这样做,我不得不删除NLog.Extensions,因为它给了我这个错误:NLOG与.net核心1.0 - LayoutRenderer无法找到

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) 

一切编译罚款,但在运行时,我得到的内部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" 
     ] 
    } 
} 
+0

哪个构架目标定制NLOG分机图书馆吗?(project.json) – Julian

+0

嗨@Julian。我希望你会看到这个问题。我想如果有人知道答案,你会的。我添加了相关的project.json部分。谢谢! –

回答

1

假设你我们正在使用this NLog.Extensions package - 它不支持NETSTANDARD,所以你不能用netcoreapp1.0

升级为使用它ASP.NET(到ASP.NET Core)不是微不足道的,看起来库不再活跃。

幸运的是在NLog.Web.AspNetCore一个aspnet-user-identity,你都已经包括在内。

因此,与${aspnet-user-identity}

+0

第三次尝试...:O – Julian

+0

谢谢。自定义LayoutRenderer实际上是让过去的$ {ASPNET用户身份}(这是一个电子邮件地址),因为我们需要记录用户的ID而不是,因为电子邮件地址会随时间改变。我想我必须找出另一种方式来存储用户ID。 –

+0

如果这还不是你要找的,请随时在GitHub上创建一个问题:https://github.com/NLog/NLog – Julian

0

更换${aspnet-user-id}我不知道为什么,但是当我注释掉的电子邮件目标和记录器,自定义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="[email protected]" 
     from="[email protected]" 
     Encoding="UTF-8" 
     smtpUsername="[email protected]" 
     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) 
+1

这是因为NLog for .NET Core不支持电子邮件目标,因为微软没有移植system.mail。请检查https://github.com/NLog/NLog/wiki/platform-support – Julian

+0

谢谢@Julian。您对NLog的帮助和工作无疑是值得赞赏的。 –

0

在根据节点添加您的NLog.config文件下列

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

希望这回答了你的quesetion。

+0

你可以详细说明该行的功能吗? –