1.打开NuGet包管理器,下载工具包
(1) 下载NLog和Nlog.Database包
NLog版本:5.2.4
Nlog.Database版本:5.2.4, 如果要将log数据写到Oracle中,必须要下载此包
(2)下载Oracle.ManagedDataAccess包
需要用此包连接oracle数据库, .net452最高兼容的版本是19.20
(3)下载NLog.Config
会自动生成文件NLog.config和NLog.xsd
2.配置Nlog.config
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="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.xsd NLog.xsd"
autoReload="true"
throwExceptions="true"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<!-- optional, add some variables
https://github.com/nlog/NLog/wiki/Configuration-file#variables
-->
<variable name="myvar" value="myvalue"/>
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information 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 possible layout renderers.
-->
<target name="console" xsi:type="Console" layout="${longdate}|${level}|${message}"/>
<!--Write events to a file with the date in the filename.-->
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
<!--存入Oracle数据库-->
<target name="database" xsi:type="Database"
dbProvider="Oracle.ManagedDataAccess.Client"
connectionString="Data Source=IP:PORT/xx;user id=xx;password=xx"
commandText="INSERT INTO LOG (
ID, LOG_DATE, LOG_LEVEL, LOG_MESSAGE, LOGGER, LOG_CALLSITE, LOG_EXCEPTION)
values (
log_seq.nextval,to_date(substr(:log_date,1,19),'yyyy-mm-dd hh24:mi:ss'), :Log_Level, :Log_Message,:Logger, :Log_Callsite, :Log_Exception)"
>
<parameter name=":log_date" layout="${longdate}" />
<parameter name=":Log_Level" layout="${level}" />
<parameter name=":Log_Message" layout="${message}" />
<parameter name=":Logger" layout="${logger}" />
<parameter name=":Log_Callsite" layout="${callsite}" />
<parameter name=":Log_Exception" layout="${exception:tostring}" />
</target>
</targets>
<rules>
<!-- add your logging rules here -->
<!--
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" />
-->
<logger name="*" writeTo="console"/>
<logger name="*" writeTo="f"/>
<logger name="*" writeTo="database"/>
</rules>
</nlog>
3. 在程序中调用
using NLog;
namespace testNlog
{
public class Program
{
public static Logger nlog = LogManager.GetCurrentClassLogger();
public static void Main(string[] args)
{
nlog.Trace("Trace");
nlog.Info("info");
nlog.Debug("Debug");
}
}
}
4.调用结果
控制台:
数据库:
5. 如果想使用NLog记录自定义的内容到数据库,可以使用以下方法:
①NLog.config
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="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.xsd NLog.xsd"
autoReload="true"
throwExceptions="true"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<!-- optional, add some variables
https://github.com/nlog/NLog/wiki/Configuration-file#variables
-->
<variable name="myvar" value="myvalue"/>
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information 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 possible layout renderers.
-->
<target name="console" xsi:type="Console" layout="${longdate}|${level}|${message}"/>
<!--Write events to a file with the date in the filename.-->
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
<!--存入Oracle数据库-->
<target name="database" xsi:type="Database"
dbProvider="Oracle.ManagedDataAccess.Client"
connectionString="Data Source=IP:PORT/xx;user id=xx;password=xx"
commandText="INSERT INTO LOGS (LOG_DATE, LOG_LEVEL, MESSAGE,LOG_TRACE) values (to_date(substr(:log_date,1,19),'yyyy-mm-dd hh24:mi:ss'), :Log_Level, :Log_Message, :Log_Trace)"
>
<parameter name=":log_date" layout="${longdate}" />
<parameter name=":Log_Level" layout="${level}" />
<parameter name=":Log_Message" layout="${event-context:item=message}" />
<parameter name=":Log_Trace" layout="${event-context:item=trace}" />
</target>
</targets>
<rules>
<!-- add your logging rules here -->
<!--
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" />
-->
<logger name="*" writeTo="console"/>
<logger name="*" writeTo="f"/>
<logger name="*" writeTo="database"/>
</rules>
</nlog>
② 调用
try
{
string s = "";
double a = Convert.ToDouble(s);
}
catch (Exception ex)
{
LogEventInfo lei = new LogEventInfo();
lei.Properties["message"] = ex.Message;
lei.Properties["trace"] = ex.StackTrace;
lei.Level = LogLevel.Info;
nlog.Log(lei);
}
③结果