Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NLog is not logging into the database when its configured to log from code

Tags:

c#

nlog

I have following console application which implements logging using NLog using the programmatic config. Currently, It is currently not logging into the database and also it does not throw any exceptions while running it. Can anybody please let me know the reason behind it?

using NLog;
using NLog.Config;
using NLog.Targets;
using System;

namespace Test.Logger
{
    class Program
    {
        static void Main(string[] args)
        {
            var logConfig = new LoggingConfiguration();


            //dbtarget 2
            var dbTarget2 = new DatabaseTarget();
            dbTarget2.ConnectionString = "SomeConnectionString";
            dbTarget2.CommandText = @"INSERT INTO [dbo].[LogAppDetail]
                                                   ([LogAppIS]
                                                   ,[LogAppId]
                                                   ,[DN]
                                                   ,[DV])
                                             VALUES
                                                   (@LogAppIS
                                                   ,@LogAppId
                                                   ,@DN
                                                   ,@DV)";

            dbTarget2.Parameters.Add(new DatabaseParameterInfo("@LogAppIS", new NLog.Layouts.SimpleLayout("${event-properties:LogAppIS}")));
            dbTarget2.Parameters.Add(new DatabaseParameterInfo("@LogAppId", new NLog.Layouts.SimpleLayout("${event-properties:LogAppId}")));
            dbTarget2.Parameters.Add(new DatabaseParameterInfo("@DN", new NLog.Layouts.SimpleLayout("${event-properties:DN}")));
            dbTarget2.Parameters.Add(new DatabaseParameterInfo("@DV", new NLog.Layouts.SimpleLayout("${event-properties:DV}")));

            logConfig.AddTarget("dbTarget2", dbTarget2);
            // add rules
            var rule2 = new LoggingRule("LogAppDetail", LogLevel.Trace, dbTarget2);
            logConfig.LoggingRules.Add(rule2);
            LogManager.Configuration = logConfig;

            LogEventInfo theEvent = new LogEventInfo(LogLevel.Debug, "", "Pass my custom value");
            theEvent.Properties["LogAppIS"] = "12345698";
            theEvent.Properties["LogAppId"] = "1235";
            theEvent.Properties["DN"] = "DN";
            theEvent.Properties["DV"] = "DV";
            var log = NLog.LogManager.GetLogger("LogAppDetail");
            log.Log(theEvent);
        }
    }
}
like image 205
coolswastik Avatar asked Oct 27 '25 18:10

coolswastik


2 Answers

To any person that's still struggling with this, as stated here, in order to make this work from NLog 5.0 and newer versions you must include the nuget package Nlog.Database

So, this should do the magic:

<PackageReference Include="NLog.Database" Version="5.0.2" />
like image 189
Carlos Álvarez Gonzalez Avatar answered Oct 29 '25 08:10

Carlos Álvarez Gonzalez


I figured out the reason the above code was not logging into the database. I was missing system.data.sqlclient package in my .net core project. I figured it out by turning following exceptions in the application.

LogManager.ThrowExceptions = true;
LogManager.ThrowConfigExceptions = true;
like image 29
coolswastik Avatar answered Oct 29 '25 09:10

coolswastik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!