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