Base solution for your next web application
Open Closed

Logging to data using NLog #3517


User avatar
0
krileo created

Hi.

We are using NLog instead of Log4net and it is working just fine - but we can not get logging to a database to work.

We are using a standard nlog configuration which works just fine in all our other systems which are not based on ASP.NET Zero. But when we try to do it in ASP.NET Zero we get the following error (when setting throwExceptions="true" in the nlog config)

"NLog.NLogConfigurationException: 'Connection string 'nlogdatabase' is not declared in <connectionStrings /> section.'"

The connection string IS configured correctly in the nlog.conf. We also tried to add it to the appsettings.json using this article but we get an exception when we try to set the connection string.

<a class="postlink" href="https://damienbod.com/2016/09/22/setting-the-nlog-database-connection-string-in-the-asp-net-core-appsettings-json/">https://damienbod.com/2016/09/22/settin ... ings-json/</a>

We even tried to set it in web.config and finally tried a nasty hack using reflection to add the connection string at runtime to System.Configuration.ConfigurationManager which got us past the initial error but then throw a null reference exception somewhere in the NLog / Castle frameworks.

Could you kindly guide us on the right way to get logging to database to work with NLog?

Regards

Kristian Leonhard


1 Answer(s)
  • User Avatar
    0
    alper created
    Support Team

    hi

    do you have System.Data.SqlClient dependency in the project? there must be line in csproj <PackageReference Include="System.Data.SqlClient" Version="4.4.0" />

    and also NLog.Extensions.Logging dependency.

    There is an internal log file , so that if something is wrong with the logging configuration, you can find out why.

    <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:\git\damienbod\AspNetCoreNlog\Logs\internal-nlog.txt">
    

    The nlog.config also needs to be added to the publishOptions in the csproj file.

    Be sure you created the nlog table in database. The table in the database must match the configuration defined in the nlog.config file.

    Please read the steps mentioned in the below link <a class="postlink" href="https://github.com/damienbod/AspNetCoreNlog">https://github.com/damienbod/AspNetCoreNlog</a>