Ivan
Ivan

Reputation: 109

Logging with NLog and runtime parameters to Database

I am trying to log some info to database with NLog, but I can retrieve some DB parameters only at runtime. Tried few solutions, but unsuccessfully. There is my NLog.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">

  <variable name="sqlDate" value="[${date:format=yyyy-MM-dd HH\:mm\:ss\.fff}]"/>

  <targets>
    <target name="dataBase"
        xsi:type="Database"
        dbProvider="System.Data.SqlClient"
        connectionString="${gdc:item=myConnectionstring}"
        dbDatabase="${gdc:item=myDataBase}">
      <commandText>
        INSERT INTO [TABLE_NAME]([COL1], [COL2] )
        VALUES(@val1, @val2)
      </commandText>
      <parameter name="@val1" layout="${event-context:item=val1}"/>
      <parameter name="@val2" layout="${event-context:item=val2}"/>
    </target>
  </targets>

  <rules>
    <logger name="_dataBase" levels="Info" writeTo="dataBase" final="true" />
  </rules>
</nlog>

And there is my .NET C# code:

public static class Log
{
    private static NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger();
    private static Config _config = LoadConfig();

    public static void Info(string val1, string val2)
    {
        DatabaseTarget databaseTarget = (NLog.Targets.DatabaseTarget)NLog.LogManager.Configuration.FindTargetByName("dataBase");
        databaseTarget.ConnectionString = _config.ConnectString;
        databaseTarget.DBDatabase = _config.DBName;
        NLog.LogManager.ReconfigExistingLoggers();

        LogEventInfo info = new LogEventInfo(LogLevel.Info, "_dataBase", "Test_message");
        info.Properties["val1"] = val1;
        info.Properties["val2"] = val2;

        _logger.Log(info);
    }
}

And it just do nothing: not crashing, not writing any data to DB. Here is my internal NLog log: http://pastebin.com/0tLi9zJ1
How can I fix this?

P.S. Sorry for my last try to ask this question, it looks like I forgot to put to it some necessary information.

Upvotes: 4

Views: 6960

Answers (1)

Ivan
Ivan

Reputation: 109

So, the working code is:

<?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">

  <variable name="sqlDate" value="${date:format=yyyy-MM-dd HH\:mm\:ss\.fff}"/>

  <targets>
    <target name="dataBase"
        xsi:type="Database"
        dbProvider="System.Data.SqlClient"
        connectionString="${gdc:item=myConnectionString}"
        dbDatabase="${gdc:item=myDataBase}">
      <commandText>
        INSERT INTO [TABLE_NAME]([COL1], [COL2] )
        VALUES(@val1, @val2)
      </commandText>
      <parameter name="@val1" layout="${event-context:item=val1}"/>
      <parameter name="@val2" layout="${event-context:item=val2}"/>
    </target>
  </targets>

  <rules>
    <logger name="_dataBase" levels="Info" writeTo="dataBase" final="true" />
  </rules>
</nlog>
public static class Log
{
    private static Config _config = LoadConfig();

    public static void Info(string val1, string val2)
    {
          NLog.Logger logger = NLog.LogManager.GetLogger("_dataBase");

          GlobalDiagnosticsContext.Set("myConnectionString", _config.ConnectString);
          GlobalDiagnosticsContext.Set("myDataBase", _config.DBName);

          LogEventInfo info = new LogEventInfo(LogLevel.Info, "_dataBase", "Test_message");
          info.Properties["val1"] = val1;
          info.Properties["val2"] = val2;

          logger.Log(info);
    }
}

Upvotes: 3

Related Questions