3

This is very related to this question, but for .net Core. I have a custom layout renderer that adds UserId and it worked until I tried to upgrade from "net461" in project.json to "netcoreapp1.0". When I did that, I had to remove NLog.Extensions because it gave me this error:

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)

Everything compiles fine, but at runtime, I get this error in the internal-nlog.txt log file, and of course, it doesn't hit my custom layout renderer.:

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

Is there a replacement for NLog.Extensions, or am I missing something?

Here are the references from project.json:

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

I have everything configured in Startup.cs and NLog.config exactly the same as it was before changing framework versions. If it's helpful to post those, let me know and I'll do that. I appreciate the help.

Edit: Added project.json contents

"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"
        ]
    }
}
Community
  • 1
  • 1
Adrian Carr
  • 3,061
  • 1
  • 34
  • 38
  • Which frameworks target your custom NLog ext library ?(project.json) – Julian Nov 19 '16 at 20:13
  • Hi @Julian. I was hoping you would see this question. I figured if anyone knew the answer, you would. I added the relevant project.json parts. Thanks! – Adrian Carr Nov 21 '16 at 01:53

3 Answers3

2

Assuming you we're using this NLog.Extensions package - it's not supporting NETSTANDARD, so you can't use it with netcoreapp1.0

Upgrading for ASP.NET (to ASP.NET Core) isn't trivial and it seems that the library isn't active anymore.

Fortunately there is a aspnet-user-identity in NLog.Web.AspNetCore, which you are already have included.

So replace ${aspnet-user-id} with ${aspnet-user-identity}

Julian
  • 33,915
  • 22
  • 119
  • 174
  • Third try... :O – Julian Nov 21 '16 at 21:12
  • Thanks. The custom LayoutRenderer is actually to get past the${aspnet-user-identity} (which is an email address) because we need to log the user's id instead, since email addresses can change over time. I guess I'll have to figure out another way to store the user id. – Adrian Carr Nov 21 '16 at 21:53
  • If this still not what you're looking for, feel free to create an issue on GitHub: https://github.com/NLog/NLog – Julian Nov 21 '16 at 22:32
0

I don't know why, but when I commented out the email target and logger, the custom LayoutRenderer started working again. For reference, here is the current nlog.config file, with email stuff commented out.

<?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>

If I uncomment the email part, I get the error below, which doesn't say anything about the email section being a problem. The only reason I even tried without the email section was because I realized that I used to get emails when an exception happened, and now I don't, and was trying to fix that.

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)
Adrian Carr
  • 3,061
  • 1
  • 34
  • 38
  • 1
    This is because the email target isn't supported in NLog for .NET Core, as Microsoft didn't ported system.mail. Please check https://github.com/NLog/NLog/wiki/platform-support – Julian Nov 24 '16 at 19:01
  • Thanks @Julian. Your help and your work on NLog is certainly appreciated. – Adrian Carr Nov 25 '16 at 17:06
0

In your NLog.config file under node add following

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

Hope this answers your quesetion.

K. Kedar
  • 1
  • 2