1.输出到服务台
<span style="font-size:18px;"><?xml version="1.0"encoding="utf-8" ?>
<nlogxmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsdNLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off"internalLogFile="c:\temp\nlog-internal.log" >
<!-- optional, add some variabeles
https://github.com/nlog/NLog/wiki/Configuration-file#variables
-->
<variable name="myvar" value="myvalue"/>
<targets>
<!--添加一个新条目让日志输出到控制台中,并添加必要的输出布局(layout)-->
<target name="console" xsi:type="Console"layout="${longdate}|${level}|${message}"/>
<!--说明 xsi:type="Console"指定输出到控制台;layout指定输出文件内容的样式${longdate}是日期(具体到毫秒),${level}是日志的等级;${message}是具体要输出的内容。-->
</targets>
<rules>
<!--添加必要的规则-->
<logger name="*" writeTo="console"/>
<!--我个人设置的规则是,将所有记录的信息输出至控制台-->
</rules>
</nlog></span>
2.输出到文件
<span style="font-size:18px;"><targets>
<!--说明 xsi:type="File"指定输出到文件类型;fileName指定输出文件的存放位置和文件名(可自定义),其中
${basedir}是程序所在的路径;
${shortdate}是日期(具体到日)。
layout指定输出文件内容的样式
${level}是日志的等级;
${longdate}是日期(具体到毫秒),
${message}是具体要输出的内容。-->
<target name="Info" xsi:type="File"fileName="${basedir}/log/test.${shortdate}.log"layout="${longdate} [${level}]: ${message}"/>
</targets>
<rules>
<logger name="*" writeTo="Info"/>
</rules></span>
3.输出到数据库
<span style="font-size:18px;"><?xml version="1.0"encoding="utf-8" ?>
<nlogxmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsdNLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<!-- optional, add some variabeles
https://github.com/nlog/NLog/wiki/Configuration-file#variables
-->
<variable name="myvar" value="myvalue"/>
<!--
Seehttps://github.com/nlog/nlog/wiki/Configuration-file
forinformation on customizing logging rules and outputs.
-->
<targets>
<!--
add your targets here
See https://github.com/nlog/NLog/wiki/Targets for possible targets.
See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possiblelayout renderers.
-->
<!--添加一个新条目让日志输出到控制台中,并添加必要的输出布局(layout)-->
<!--<target name="console" xsi:type="Console"layout="${longdate}|${level}|${message}"/>-->
<!--说明 xsi:type="Console"指定输出到控制台;layout指定输出文件内容的样式${longdate}是日期(具体到毫秒),${level}是日志的等级;${message}是具体要输出的内容。-->
<!--Writing events to the a file with the date in the filename.-->
<!--<target name="Info" xsi:type="File"fileName="${basedir}/log/test.${shortdate}.log"layout="${longdate} [${level}]:${message}"/>-->
<target xsi:type="Database" name="database"connectionString="
Data Source=(local);InitialCatalog=NLogtest;Persist Security Info=True;
User ID=sa;Password=123456"commandText="insert into NLog_Log([CreateOn],[Origin],[LogLevel],[Message], [Exception],[StackTrace]) values (getdate(), @origin, @logLevel,@message,@exception, @stackTrace)">
<!--日志来源-->
<parameter name="@origin" layout="${callsite}"/>
<!--日志等级-->
<parameter name="@logLevel"layout="${level}"/>
<!--日志消息-->
<parameter name="@message"layout="${message}"/>
<!--异常信息-->
<parameter name="@exception"layout="${exception}" />
<!--堆栈信息-->
<parameter name="@stackTrace"layout="${stacktrace}"/>
</target>
</targets>
<rules>
<!-- add your logging rules here -->
<!--<logger name="*" writeTo="Info"/>-->
<logger name="*" minlevel="Debug"writeTo="database"/>
<!--
Write all events with minimal level of Debug (So Debug, Info, Warn,Error and Fatal, but not Trace) to"f"
<logger name="*" minlevel="Debug"writeTo="f" />
-->
</rules>
</nlog> </span>