Some time ago want to realize this function online to find a lot of information, now tidy up the release, I hope to give you a little help. The first is the choice of dependencies:
About the Nlog version is not up-to-date because the latest version is a bit of a problem I tried not to support, so choose these versions, MySQL version is because the Nlog configuration is not supported by default MySQL direct connection. This is the preparatory work.
Then is the configuration of the Nlog.config:
<target xsi:type= "Null" name= "Blackhole"/>
<target xsi:type= "Database" name= "database" dbprovider= "Mysql.data.mysqlclient.mysqlconnection,mysql.data" connectionstring= "Server=;user id=;p assword=;D atabase=testing;" commandtext= "INSERT into Access_log (app_key,access _IP,ACCESS_TIME,MSG) VALUES (@app_key, @access_ip, @access_time, @msg) ">
<parameter name= "App_key" layout= "${event-context:item=app_key}"/>
<parameter name= "access_ip" layout= "${event-context:item=access_ip}"/>
<parameter name= "Access_time" layout= "${event-context:item=access_time}"/>
<parameter name= "msg" layout= "${event-context:item=msg}"/>
</target>
This parameter attribute is optional last necessary, the log log content needs its own definition of the format and it is the same, no need for the log he will not record
<rules>
<logger name= "*" writeto= "database"/>
</rules>
This is the rule.
Logeventinfo ei = new Logeventinfo ();
Ei. level = Loglevel.info;
Ei. properties["App_key"] = param["AppKey"]. ToString ();
Ei. properties["access_ip"] = Accessip;
Ei. properties["Access_time"] =accessdate;
Ei. properties["MSG"] = log;
_logger. Log (EI);
This is the log format. Other questions actually have the answer on the net, the main problem is that the Dbprovider attribute cannot write MySQL directly.
What shortcomings and suggestions can and I mention, common progress!
. Net Core nlog logging to MySQL