James Fallon
James Fallon

Reputation: 164

Programmatically configure NLog

i have these four parameters that return nothing to database : Why this is happening and how to solve it ?

This is the full code :

https://www.codepile.net/pile/z1lol0Gb

Database is here :

https://www.codepile.net/pile/01JgbjXV

Part of my code

var dbTarget = new DatabaseTarget ();
dbTarget.ConnectionString = @"data source=.;initial catalog=Database5;user id=App;password=App;";
dbTarget.CommandText =
    @"insert into Logs (Level,CallSite,Type,Message,StackTrace,InnerException,AdditionalInfo) values (
         @level,
         @callSite,
         @type,
         @message,
         @stackTrace,
         @innerException,
         @additionalInfo
         );";
dbTarget.Parameters.Add (new DatabaseParameterInfo ("@level", new NLog.Layouts.SimpleLayout ("${level}")));
dbTarget.Parameters.Add (new DatabaseParameterInfo ("@callSite", new NLog.Layouts.SimpleLayout ("${callsite}")));
dbTarget.Parameters.Add (new DatabaseParameterInfo ("@type", new NLog.Layouts.SimpleLayout ("${exception:format=type}")));
dbTarget.Parameters.Add (new DatabaseParameterInfo ("@message", new NLog.Layouts.SimpleLayout ("${exception:format=message}")));
dbTarget.Parameters.Add (new DatabaseParameterInfo ("@stackTrace", new NLog.Layouts.SimpleLayout ("${${exception:format=stackTrace}}")));
dbTarget.Parameters.Add (new DatabaseParameterInfo ("@innerException", new NLog.Layouts.SimpleLayout ("${exception:format=:innerFormat=ShortType,Message,Method:MaxInnerExceptionLevel=1:InnerExceptionSeparator}")));
dbTarget.Parameters.Add (new DatabaseParameterInfo ("@additionalInfo", new NLog.Layouts.SimpleLayout ("${message}")));

I have the same database that exist here :

https://knightcodes.com/.net/2016/05/25/logging-to-a-database-wth-nlog.html

This how they look in database

enter image description here

Database should look like this : enter image description here

Upvotes: 0

Views: 854

Answers (2)

Julian
Julian

Reputation: 36720

You need to proper log your exception.

You code shows:

Logger logger1 = LogManager.GetLogger ("Logger1");
// Log something..
logger1.Error("Test Logger1");

but the Exception should be the first argument, see How to log exceptions in NLog

Logger logger1 = LogManager.GetLogger ("Logger1");
// Log something..
logger1.Error(new Exception("my error"), "Test Logger1");

Upvotes: 1

mr_cysio
mr_cysio

Reputation: 11

Inner exception can be empty so you can have empty values in your DB (but you must verify if it will save if inner exception is present). As for other properties I don't think someone can help you with sample you provided. Problem seems to lay in exception handling and using logger. However I can't verify this with your sample. Here is problem that seems similar to yours. You can also read about layour renderer and look for answers on NLog wiki. If you are not already doing this consider using NLog config file to have everything concerning configuration in one place.

Upvotes: 0

Related Questions