[转]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">详细日志</param>
 64         /// <param name="IP">ip地址</param>
 65         /// <param name="USERNAME">用户名</param>
 66         /// <param name="LOGGER">LOGGER</param>
 67         /// <param name="Level">日志级别</param>
 68         public void Process(string APPNAME, string MODULENAME, string PROCNAME, OperationType OPERATIONTYPE, string LOGMESSAGE, string IP, string USERNAME, string LOGGER, LogLevel Level)
 69         {
 70             if (LOGMESSAGE.Length > 3000)
 71             {
 72                 LOGMESSAGE = LOGMESSAGE.Substring(0, 3000);
 73             }
 74             LogEventInfo lei = new LogEventInfo();
 75             lei.Properties["ID"] = Guid.NewGuid().ToString("D");
 76             lei.Properties["APPNAME"] = APPNAME;
 77             lei.Properties["MODULENAME"] = MODULENAME;
 78             lei.Properties["PROCNAME"] = PROCNAME;
 79             lei.Properties["OPERATIONTYPE"] = OPERATIONTYPE;
 80             lei.Properties["LOGMESSAGE"] = LOGMESSAGE;
 81             lei.Properties["IP"] = IP;
 82             lei.Properties["USERNAME"] = USERNAME;
 83             lei.Properties["LOGGER"] = LOGGER;
 84             lei.Level = Level;
 85             _logger.Log(lei);
 86         }
 87         #endregion      
 88     }
 89     /// <summary>
 90     /// 操作类型枚举
 91     /// </summary>
 92     public enum OperationType
 93     {
 94         /// <summary>
 95         /// 保存或添加
 96         /// </summary>
 97         [System.ComponentModel.Description("保存或添加")]
 98         ADD,
 99         /// <summary>
100         /// 更新
101         /// </summary>
102         [System.ComponentModel.Description("更新")]
103         UPDATE,
104         /// <summary>
105         /// 审核
106         /// </summary>
107         [System.ComponentModel.Description("审核")]
108         AUDIT,
109         /// <summary>
110         /// 删除
111         /// </summary>
112         [System.ComponentModel.Description("删除")]
113         DELETE,
114         /// <summary>
115         /// 读取/查询
116         /// </summary>
117         [System.ComponentModel.Description("读取/查询")]
118         RETRIEVE,
119         /// <summary>
120         /// 登录
121         /// </summary>
122         [System.ComponentModel.Description("登录")]
123         LOGIN,
124         /// <summary>
125         /// 查看
126         /// </summary>
127         [System.ComponentModel.Description("查看")]
128         LOOK
129     }
复制代码

5、asp.net中的调用列子:

1             Logger.Default.Process("系统管理", "权限管理", "用户管理", OperationType.RETRIEVE, "测试写入", Logger.DefaultIP, Logger.DefaultUser, this.GetType().ToString(), NLog.LogLevel.Error);
2             Logger.Default.Process("系统管理", "权限管理", "用户管理", OperationType.RETRIEVE, "测试写入", Logger.DefaultIP, Logger.DefaultUser, this.GetType().ToString());

 

posted on 2017-05-24 17:12  freeliver54  阅读(318)  评论(1编辑  收藏  举报

导航