2016-11-18 78 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

你可以詳細說明該行的功能嗎? –