NLog custom log content, write log to database, modify the reason that Nlog.config does not work

Source: Internet
Author: User

Nlog installation Please Baidu, I installed is 3.2. Nlog can write to files, databases, mails, etc., want to know Baidu, here is how to write the database, and often encountered problems:

1. Layout Render

NLog built a lot of log content format, look like ${longdate} and the like, see: Https://github.com/NLog/NLog/wiki/Layout-Renderers

2. If we want to customize the log:

<target xsi:type="Database"Name="Logonlog"connectionStringName ="DataBase"><commandText>Insert into MyLog (operator_name,operator_type,ip,datetime,operation) Values (@Operator_name, @Operator_Type, @IP , @DateTime, @Operation);</commandtext><parameter name ="@Operator_name"Layout ="${event-context:item=operator_name}"/><parameter name ="@Operator_Type"Layout ="${event-context:item=operator_type}"/><parameter name ="@DateTime"Layout ="${event-context:item=datetime}"/><parameter name ="@IP"Layout ="${event-context:item=ip}"/><parameter name ="@Operation"Layout ="${event-context:item=operation}"/></target></targets><rules><logger name="*"Minlevel="Trace"writeto="Logonlog"/></rules>
${event-context:item=operation}: The following "Operation" will be generated logger a key called "Operation", we can be in the code to assign him value, see: https:// Github.com/nlog/nlog/wiki/eventcontext-layout-renderer. This is what I have in my code:

Logger Logger = Logmanager.getcurrentclasslogger (); Cannot use Logger Logger =new Logger (); Error: "NLog.Logger.Logger ()" is inaccessible because it is protected by a level
Logeventinfo lei = new Logeventinfo ();
Lei. properties["operator_name"] = Opeator_name;
Lei. properties["Operator_type"] = Operator_type;
Lei. properties["DateTime"] = DateTime.Now;
Lei. properties["IP"] = Getclientiporadd.getip ();
Lei. properties["operation"] = operation;
Lei. level = Loglevel.info;
Logger. Log (LEI);

3. Important: When we modify the Nlog.config file, the modification will not work until the solution is recompiled. This should be a huge pit where many people use Nlog unsuccessfully!

As described in the web and Nlog documentation: adding the aotureload= "true" attribute, does not work, do not know whether I have not noticed the method.

4, the collection of explanation nlog more detailed article:

Http://www.cnblogs.com/sorex/archive/2013/01/31/2887174.html

Http://www.cnblogs.com/Gyoung/archive/2012/10/18/2729613.html

http://blog.csdn.net/viviachen/article/details/19171661

NLog custom log content, write log to database, modify the reason that Nlog.config does not work

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.