[转]NLog 自定义字段 写入 oracle
本文转自:http://www.cnblogs.com/skyapplezhao/p/5690695.html
1、通过Nuget安装NLog 下载,简单入门 请参照 我刚才转的几篇文章,下面我直接贴代码
2、建表语句
1 create table TBL_LOG
2 (
3 id VARCHAR2(60) not null,
4 appname VARCHAR2(20),
5 modulename VARCHAR2(30),
6 procname VARCHAR2(30),
7 operationtype VARCHAR2(20),
8 logger VARCHAR2(500),
9 logmessage VARCHAR2(3000) not null,
10 ip VARCHAR2(32),
11 longdate VARCHAR2(36),
12 username VARCHAR2(36),
13 createdate DATE default sysdate,
14 loglevel VARCHAR2(12)
15 )
3、NLog.config,正式运行 throwExceptions="false"
1 <?xml version="1.0" encoding="utf-8" ?>
2 <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
5 autoReload="true"
6 throwExceptions="true"
7 internalLogLevel="Info" internalLogFile="d:\work\log1.txt">
8 <targets async="true">
9 <target name="database" xsi:type="Database" keepConnection="false" useTransactions="true"
10 dbProvider="System.Data.OracleClient.OracleConnection,System.Data.OracleClient, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
11 connectionString="Data Source=ORCL;User Id=oracle;Password=oracle;Integrated Security=no;"
12 commandText="insert into TBL_LOG(APPNAME,MODULENAME,PROCNAME,OPERATIONTYPE,LOGGER,LOGMESSAGE,IP,LONGDATE,USERNAME,LOGLEVEL) values(:APPNAME,:MODULENAME,:PROCNAME,:OPERATIONTYPE,:LOGGER,:LOGMESSAGE,:IP,:TIME_STAMP,:USERNAME,:LOGLEVEL)">
13 <parameter name="APPNAME" layout="${event-context:item=APPNAME}" />
14 <parameter name="MODULENAME" layout="${event-context:item=MODULENAME}" />
15 <parameter name="PROCNAME" layout="${event-context:item=PROCNAME}" />
16 <parameter name="OPERATIONTYPE" layout="${event-context:item=OPERATIONTYPE}" />
17 <parameter name="LOGGER" layout="${event-context:item=LOGGER}" />
18 <parameter name="LOGMESSAGE" layout="${event-context:item=LOGMESSAGE}" />
19 <parameter name="IP" layout="${event-context:item=IP}" />
20 <parameter name="TIME_STAMP" layout="${longdate}" />
21 <parameter name="USERNAME" layout="${event-context:item=USERNAME}" />
22 <parameter name="LOGLEVEL" layout="${level:uppercase=true}" />
23 </target>
24 </targets>
25 <rules>
26 <logger name="*" minlevel="Trace" writeTo="database" />
27 </rules>
28 </nlog>
4、简单封装
1 public class Logger
2 {
3 #region 初始化
4 /// <summary>
5 /// 数据错误无法获取用户时使用
6 /// </summary>
7 public static string DefaultUser = "system";
8 /// <summary>
9 /// 默认地址
10 /// </summary>
11 public static string DefaultIP = "127.0.0.1";
12 NLog.Logger _logger;
13 private Logger(NLog.Logger logger)
14 {
15 _logger = logger;
16 }
17 public Logger(string name) : this(LogManager.GetLogger(name))
18 { }
19 public static Logger Default { get; private set; }
20 static Logger()
21 {
22 Default = new Logger(LogManager.GetCurrentClassLogger());
23 }
24 #endregion
25 #region Process日志写入
26 /// <summary>
27 /// 日志写入
28 /// </summary>
29 /// <param name="APPNAME">菜单大类</param>
30 /// <param name="MODULENAME">二级菜单</param>
31 /// <param name="PROCNAME">本级菜单</param>
32 /// <param name="OPERATIONTYPE">操作类型</param>
33 /// <param name="LOGMESSAGE">详细日志</param>
34 /// <param name="IP">ip地址</param>
35 /// <param name="USERNAME">用户名</param>
36 /// <param name="LOGGER">LOGGER</param>
37 public void Process(string APPNAME, string MODULENAME, string PROCNAME, OperationType OPERATIONTYPE, string LOGMESSAGE, string IP, string USERNAME, string LOGGER)
38 {
39 if (LOGMESSAGE.Length > 3000)
40 {
41 LOGMESSAGE = LOGMESSAGE.Substring(0, 3000);
42 }
43 LogEventInfo lei = new LogEventInfo();
44 lei.Properties["ID"] = Guid.NewGuid().ToString("D");
45 lei.Properties["APPNAME"] = APPNAME;
46 lei.Properties["MODULENAME"] = MODULENAME;
47 lei.Properties["PROCNAME"] = PROCNAME;
48 lei.Properties["OPERATIONTYPE"] = OPERATIONTYPE;
49 lei.Properties["LOGMESSAGE"] = LOGMESSAGE;
50 lei.Properties["IP"] = IP;
51 lei.Properties["USERNAME"] = USERNAME;
52 lei.Properties["LOGGER"] = LOGGER;
53 lei.Level = LogLevel.Info;
54 _logger.Log(lei);
55 }
56 /// <summary>
57 /// 日志写入,自己传日志类别
58 /// </summary>
59 /// <param name="APPNAME">菜单大类</param>
60 /// <param name="MODULENAME">二级菜单</param>
61 /// <param name="PROCNAME">本级菜单)</param>
62 /// <param name="OPERATIONTYPE">操作类型</param>
63 /// <param name="LOGMESSAGE">详细日志