I have a C#.Net 4.8 Console Application where I am trying to use NLog for logging to a MSSQL database.
My application has the following:
private static Logger nLogger = LogManager.GetLogger("myLogger");
static void Main()
{
nLogger.Info("ConsoleApp Started");
// MAIN code of application
nLogger.Info("ConsoleApp Ended");
LogManager.Flush();
}
My app.config file has the following:
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="ConsoleApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true">
<!-- the targets to write to -->
<targets async="true">
<target name="db" xsi:type="Database"
connectionString="server=127.0.0.1\test;Database=TicketDB_Test;user id=USER;password=PASSWORD"
commandType="StoredProcedure"
commandText="[dbo].[NLog_AddEntry]">
<parameter name="@machineName" layout="${machinename}" />
<parameter name="@level" layout="${level}"/>
<parameter name="@message" layout="${message}"/>
<parameter name="@logger" layout="${processname}" />
<parameter name="@loggertype" layout="${literal:text=ConsoleApplication}" />
<parameter name="@properties" layout="${all-event-properties:separator=|}" />
<parameter name="@callsite" layout="${callsite}"/>
<parameter name="@linenumber" layout="${callsite-linenumber}" />
<parameter name="@exception" layout="${exception:tostring}"/>
<parameter name="@stackTrace" layout="${stacktrace}"/>
</target>
</targets>
<!-- rules to map from logger name to target -->
<rules>
<logger name="myLogger" minlevel="Trace" writeTo="db" />
</rules>
</nlog>
<applicationSettings>
<ConsoleApp.Properties.Settings>
//...
</ConsoleApp.Properties.Settings>
</applicationSettings>
</configuration>
The solution has the NLog and NLog.Database packages both at version 5.2.4
When the console app is run nothing is logged to the database. The stored procedure NLog_AddEntry has been checked and works.
I have ran out of ideas why NLog is not logging to the db?
@RolfKristensen Thanks for suggestions. The internal logger gave me the final answer… I had mistyped one of the parameter names in my target… great help.