. Net Core nlog logging to MySQL

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.