NLog记录日志到Oracle数据库
时间:2021-07-01 10:21:17
帮助过:78人阅读
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="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<variable name="myvar" value="myvalue"/>
<targets>
<!--普通日志-->
<target xsi:type="File" name="info" fileName="${basedir}/logs/log.log"
layout="${longdate} ${uppercase:${level}} ${message}"
maxArchiveFiles="50"
archiveAboveSize="5242880"
archiveNumbering="Rolling"
/>
<!--错误日志-->
<target xsi:type="File" name="err" fileName="${basedir}/Errors/Error.log"
layout="${longdate} ${uppercase:${level}} ${message} --${ticks}"
maxArchiveFiles="50"
archiveAboveSize="5242880"
archiveNumbering="Rolling"
/>
<!--存入Oracle数据库-->
<target name="database" xsi:type="Database"
dbProvider="Oracle.ManagedDataAccess.Client.OracleConnection, Oracle.ManagedDataAccess"
connectionString="Data Source=192.168.1.100/wifi;user id=system;password=oracle"
>
<!--这里的插入语句,注意最后不要加分号,否则会报错。另外,Oracle中需要用":",用"@"会报错-->
<commandText>
<![CDATA[
insert into bdcdataar.log4net_log(no,log_date,log_thread,log_level,log_location,log_message,log_exception,system_id) values(bdcdataar.log4net_log_seq.nextval,to_date(substr(:log_date,1,19),‘yyyy-mm-dd hh24:mi:ss‘),:log_thread,:log_level,:log_location,:log_message,:log_exception,‘共享查询‘)
]]>
</commandText>
<parameter name=":log_date" layout="${longdate}" />
<parameter name=":log_thread" layout="${threadid}" />
<parameter name=":log_level" layout="${level}" />
<parameter name=":log_location" layout="${callsite:filename=true}" />
<parameter name=":log_message" layout="${message}" />
<parameter name=":log_exception" layout="${exception:tostring}" />
</target>
</targets>
<rules>
<!--普通日志-->
<logger name="*" minlevel="Trace" maxlevel="Info" writeTo="info" />
<!--错误日志-->
<logger name="*" minlevel="Warn" maxlevel="Fatal" writeTo="err" />
<!--所有日志-->
<logger name="*" minlevel="Trace" maxlevel="Fatal" writeTo="database" />
</rules>
</nlog>
C#创建LogHelper类,当然需要Nuget引入NLog的DLL了
public class LogHelper
{
public static NLog.Logger nlog = NLog.LogManager.GetCurrentClassLogger();
/// <summary>
/// 普通的文件记录日志
/// </summary>
/// <param name="info">信息</param>
public static void WriteLog(string info)
{
nlog.Debug(info);
}
}
最重要的还是要创建数据库表了哈:
create table bdcdataar.log4net_log
(
no int primary key,--序号
log_date date,--时间
log_thread int,--线程号
log_level varchar2(10),--日志等级
log_location varchar2(500),--日志位置
log_message clob,--消息
log_exception clob,--异常
system_id varchar2(50) --系统id,用于标识不同的系统
);
create sequence bdcdataar.log4net_log_seq
minvalue 1
maxvalue 999999999
start with 1
increment by 1;
说明:
1.throwExceptions是否启用抛出内部错误功能。internalLogLevel内部日志是否开启。internalLogFile内部日志路径
NLog记录日志到Oracle数据库
标签:span off 说明 uppercase sub tostring info version nextval