c# - Logging with NLog and runtime parameters to Database -


i trying log info database nlog, can retrieve db parameters @ runtime. tried few solutions, unsuccessfully. there 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 [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 .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 nothing: not crashing, not writing data db. here internal nlog log: http://pastebin.com/0tli9zj1
how can fix this?

p.s. sorry last try ask question, looks forgot put necessary information.

so, 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 [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);     } } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -