.net-corec#-3.0nlogwindows-identity

NLog "Input string was not in a correct format." using windows-identity


I am trying to log some information to database, but being kind of new to nlog config ( until now the default config from Tutorials worked fine) I am not sure what I am missing from my config to work.

I get this error when I app starts

2022-02-24 17:06:46.1375 Error Failed loading from config file location: C:\Repos\App\bin\Debug\netcoreapp3.1\NLog.config Exception: NLog.NLogConfigurationException: Exception when parsing C:\Repos\App\bin\Debug\netcoreapp3.1\NLog.config. 
 ---> System.FormatException: Input string was not in a correct format.
   at System.Text.StringBuilder.FormatError()
   at System.Text.StringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args)
   at System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args)
   at System.String.Format(String format, Object[] args)
   at NLog.NLogConfigurationException..ctor(Exception innerException, String message, Object[] messageParameters)
   at NLog.Config.LoggingConfigurationParser.ConfigureObjectFromAttributes(Object targetObject, ILoggingConfigurationElement element, Boolean ignoreType)
   at NLog.Config.LoggingConfigurationParser.ParseTargetElement(Target target, ILoggingConfigurationElement targetElement, Dictionary`2 typeNameToDefaultTargetParameters)
   at NLog.Config.LoggingConfigurationParser.ParseTargetsElement(ILoggingConfigurationElement targetsElement)
   at NLog.Config.LoggingConfigurationParser.ParseNLogSection(ILoggingConfigurationElement configSection)
   at NLog.Config.XmlLoggingConfiguration.ParseNLogSection(ILoggingConfigurationElement configSection)
   at NLog.Config.LoggingConfigurationParser.LoadConfig(ILoggingConfigurationElement nlogConfig, String basePath)
   at NLog.Config.XmlLoggingConfiguration.ParseNLogElement(ILoggingConfigurationElement nlogElement, String filePath, Boolean autoReloadDefault)
   at NLog.Config.XmlLoggingConfiguration.ParseTopLevel(NLogXmlElement content, String filePath, Boolean autoReloadDefault)
   at NLog.Config.XmlLoggingConfiguration.Initialize(XmlReader reader, String fileName, Boolean ignoreErrors)
   --- End of inner exception stack trace ---
   at NLog.Config.XmlLoggingConfiguration.Initialize(XmlReader reader, String fileName, Boolean ignoreErrors)
   at NLog.Config.XmlLoggingConfiguration..ctor(XmlReader reader, String fileName, LogFactory logFactory)
   at NLog.Config.LoggingConfigurationFileLoader.LoadXmlLoggingConfiguration(XmlReader xmlReader, String configFile, LogFactory logFactory)
   at NLog.Config.LoggingConfigurationFileLoader.LoadXmlLoggingConfigurationFile(LogFactory logFactory, String configFile)
   at NLog.Config.LoggingConfigurationFileLoader.TryLoadLoggingConfiguration(LogFactory logFactory, String configFile, LoggingConfiguration& config)

My Config files looks like this

<?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"
      throwConfigExceptions="true"
      internalLogLevel="trace"
      internalLogFile="c:\temp\internal-nlog-AspNetCore3.txt">

    <!-- enable asp.net core layout renderers -->
    <extensions>
        <add assembly="NLog.Web.AspNetCore"/>
    </extensions>

    <!-- the targets to write to -->
    <targets>
        <!-- File Target for all log messages with basic details -->
        <target xsi:type="File" name="allfile" fileName="c:\temp\nlog-AspNetCore3-all-${shortdate}.log"
                layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />

        <!-- File Target for own log messages with extra web details using some ASP.NET core renderers -->
        <target xsi:type="File" name="ownFile-web" fileName="c:\temp\nlog-AspNetCore3-own-${shortdate}.log"
                layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}|" />

        <!--Console Target for hosting lifetime messages to improve Docker / Visual Studio startup detection -->
        <target xsi:type="Console" name="lifetimeConsole" layout="${MicrosoftConsoleLayout}" />

        <target xsi:type="database" name="database"
            layout="${longdate}|${level:uppercase=true}|${logger}|${message}|identity:${identity}|windows-identity:${windows-identity}">
            <connectionString>
                Data Source=MYCOMPUTER;Initial Catalog=DatabaseName;Integrated Security=True
            </connectionString>
            <commandText>
                INSERT INTO SystemLogging(LogDate,LogLevel,LogLogger,LogMessage,LogMachineName, LogUserName, LogCallSite, LogThread, LogException, LogStackTrace) 
                VALUES(@time_stamp, @level, @logger, @message,@machinename, @user_name, @call_site, @threadid, @log_exception, @stacktrace);
            </commandText>
            <parameter name="@time_stamp" layout="${longdate}"/>
            <parameter name="@level" layout="${level}"/>
            <parameter name="@logger" layout="${logger}"/>
            <parameter name="@message" layout="${message}"/>
            <parameter name="@machinename" layout="${machinename}"/>
            <parameter name="@user_name" layout="${windows-identity:domain=true}"/>
            <parameter name="@call_site" layout="${callsite:filename=true}"/>
            <parameter name="@threadid" layout="${threadid}"/>
            <parameter name="@log_exception" layout="${exception}"/>
            <parameter name="@stacktrace" layout="${stacktrace}"/>
        </target>
    </targets>

    <!-- rules to map from logger name to target -->
    <rules>
        <!--All logs, including from Microsoft-->
        <logger name="*" minlevel="Trace" writeTo="allfile" />

        <!--Output hosting lifetime messages to console target for faster startup detection -->
        <logger name="Microsoft.Hosting.Lifetime" minlevel="Info" writeTo="lifetimeConsole, ownFile-web" final="true" />

        <!--Skip non-critical Microsoft logs and so log only own logs-->
        <logger name="Microsoft.*" maxlevel="Info" final="true" />
        <!-- BlackHole -->

        <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
    </rules>
</nlog>

I know for a fact the problem is somewhere in line 28, layout of db connection.

layout="${longdate}|${level:uppercase=true}|${logger}|${message}|identity:${identity}|windows-identity:${windows-identity}"

If I remove this line however I get an other error

"LayoutRenderer cannot be found: 'windows-identity'"

It feels like an obvious problem, however search the internet I could not find a solution. Anyone with more experience in NLog, can you share an example of DB Logging that uses Windows-Auth for db logging and not a username/password in connection string.

I've also tried ( added '' to windows-identity, but that didn't work either )

layout="${longdate}|${level:uppercase=true}|${logger}|${message}|identity: '${identity}' | windows-identity: '${windows-identity}'"

Edit:

Updated 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"
      throwConfigExceptions="true"
      internalLogLevel="trace"
      internalLogFile="c:\temp\internal-nlog-AspNetCore3.txt">

    <!-- enable asp.net core layout renderers -->
    <extensions>
        <add assembly="NLog.Web.AspNetCore"/>
        <add assembly="NLog.WindowsIdentity"/>
    </extensions>

    <!-- the targets to write to -->
    <targets>
        <!-- File Target for all log messages with basic details -->
        <target xsi:type="File" name="allfile" fileName="c:\temp\nlog-AspNetCore3-all-${shortdate}.log"
                layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />

        <!-- File Target for own log messages with extra web details using some ASP.NET core renderers -->
        <target xsi:type="File" name="ownFile-web" fileName="c:\temp\nlog-AspNetCore3-own-${shortdate}.log"
                layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}|" />

        <!--Console Target for hosting lifetime messages to improve Docker / Visual Studio startup detection -->
        <target xsi:type="Console" name="lifetimeConsole" layout="${MicrosoftConsoleLayout}" />

        <target xsi:type="database" name="database"
            layout="${longdate}|${level:uppercase=true}|${logger}|${message}|identity:${identity}|windows-identity:${environment-user}">
            <connectionString>
                Data Source=MYCOMPUTER;Initial Catalog=DatabaseName;Integrated Security=True
            </connectionString>
            <commandText>
                INSERT INTO SystemLogging(LogDate,LogLevel,LogLogger,LogMessage,LogMachineName, LogUserName, LogCallSite, LogThread, LogException, LogStackTrace) 
                VALUES(@time_stamp, @level, @logger, @message,@machinename, @user_name, @call_site, @threadid, @log_exception, @stacktrace);
            </commandText>
            <parameter name="@time_stamp" layout="${longdate}"/>
            <parameter name="@level" layout="${level}"/>
            <parameter name="@logger" layout="${logger}"/>
            <parameter name="@message" layout="${message}"/>
            <parameter name="@machinename" layout="${machinename}"/>
            <parameter name="@user_name" layout="${windows-identity:domain=true}"/>
            <parameter name="@call_site" layout="${callsite:filename=true}"/>
            <parameter name="@threadid" layout="${threadid}"/>
            <parameter name="@log_exception" layout="${exception}"/>
            <parameter name="@stacktrace" layout="${stacktrace}"/>
        </target>
    </targets>

    <!-- rules to map from logger name to target -->
    <rules>
        <!--All logs, including from Microsoft-->
        <logger name="*" minlevel="Trace" writeTo="allfile" />

        <!--Output hosting lifetime messages to console target for faster startup detection -->
        <logger name="Microsoft.Hosting.Lifetime" minlevel="Info" writeTo="lifetimeConsole, ownFile-web" final="true" />

        <!--Skip non-critical Microsoft logs and so log only own logs-->
        <logger name="Microsoft.*" maxlevel="Info" final="true" />
        <!-- BlackHole -->

        <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
    </rules>
</nlog>

So it turns out the layout is not working. I removed it and app would start with no erros. However I still had to add a rule in order to make logs in db. Here is the working version of the nlog file.

<?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"
      throwConfigExceptions="true"
      internalLogLevel="trace"
      internalLogFile="c:\temp\internal-nlog-AspNetCore3.txt">

    <!-- enable asp.net core layout renderers -->
    <extensions>
        <add assembly="NLog.Web.AspNetCore"/>
        <add assembly="NLog.WindowsIdentity"/>
    </extensions>

    <!-- the targets to write to -->
    <targets>
        <!-- File Target for all log messages with basic details -->
        <target xsi:type="File" name="allfile" fileName="c:\temp\nlog-AspNetCore3-all-${shortdate}.log"
                layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />

        <!-- File Target for own log messages with extra web details using some ASP.NET core renderers -->
        <target xsi:type="File" name="ownFile-web" fileName="c:\temp\nlog-AspNetCore3-own-${shortdate}.log"
                layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}|" />

        <!--Console Target for hosting lifetime messages to improve Docker / Visual Studio startup detection -->
        <target xsi:type="Console" name="lifetimeConsole" layout="${MicrosoftConsoleLayout}" />

        <target xsi:type="database" name="database">
            <connectionString>
                Data Source=MYCOMPUTER;Initial Catalog=DbName;Integrated Security=True
            </connectionString>
            <commandText>
                INSERT INTO SystemLogging(LogDate,LogLevel,LogLogger,LogMessage,LogMachineName, LogUserName, LogCallSite, LogThread, LogException, LogStackTrace) 
                VALUES(@time_stamp, @level, @logger, @message,@machinename, @user_name, @call_site, @threadid, @log_exception, @stacktrace);
            </commandText>
            <parameter name="@time_stamp" layout="${longdate}"/>
            <parameter name="@level" layout="${level}"/>
            <parameter name="@logger" layout="${logger}"/>
            <parameter name="@message" layout="${message}"/>
            <parameter name="@machinename" layout="${machinename}"/>
            <parameter name="@user_name" layout="${windows-identity:domain=true}"/>
            <parameter name="@call_site" layout="${callsite:filename=true}"/>
            <parameter name="@threadid" layout="${threadid}"/>
            <parameter name="@log_exception" layout="${exception}"/>
            <parameter name="@stacktrace" layout="${stacktrace}"/>
        </target>
    </targets>

    <!-- rules to map from logger name to target -->
    <rules>
        <!--All logs, including from Microsoft-->
        <logger name="*" minlevel="Trace" writeTo="allfile" />
        <logger name="*" minLevel="Trace" writeTo="database"/>

        <!--Output hosting lifetime messages to console target for faster startup detection -->
        <logger name="Microsoft.Hosting.Lifetime" minlevel="Info" writeTo="lifetimeConsole, ownFile-web" final="true" />

        <!--Skip non-critical Microsoft logs and so log only own logs-->
        <logger name="Microsoft.*" maxlevel="Info" final="true" />
        <!-- BlackHole -->

        <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
    </rules>
</nlog>

Solution

  • The NLog Database-Target doesn't have a Layout-property, that can be assigned. Instead use input parameter-collection.

    WindowsIdentity-nuget-package is required for ${windows-identity} on NetCore, and must be included:

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

    Maybe you are looking for ${environment-user} ?

    See also: https://github.com/NLog/NLog/wiki/Windows-Identity-Layout-Renderer