• NLog 自定义字段 写入 oracle


    1、通过Nuget安装NLog 下载,简单入门 请参照 我刚才转的几篇文章,下面我直接贴代码

    2、建表语句

    create table TBL_LOG
    (
      id            VARCHAR2(60)  not null,
      appname       VARCHAR2(20),
      modulename    VARCHAR2(30),
      procname      VARCHAR2(30),
      operationtype VARCHAR2(20),
      logger        VARCHAR2(500),
      logmessage    VARCHAR2(3000) not null,
      ip            VARCHAR2(32),
      longdate      VARCHAR2(36),
      username      VARCHAR2(36),
      createdate    DATE default sysdate,
      loglevel      VARCHAR2(12)
    )

    3、NLog.config,正式运行 throwExceptions="false"

    <?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="Info" internalLogFile="d:worklog1.txt">
      <targets async="true">
        <target name="database" xsi:type="Database" keepConnection="false" useTransactions="true"
                    dbProvider="System.Data.OracleClient.OracleConnection,System.Data.OracleClient, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                    connectionString="Data Source=ORCL;User Id=oracle;Password=oracle;Integrated Security=no;"
                    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)">
          <parameter name="APPNAME" layout="${event-context:item=APPNAME}" />
          <parameter name="MODULENAME" layout="${event-context:item=MODULENAME}" />
          <parameter name="PROCNAME" layout="${event-context:item=PROCNAME}" />
          <parameter name="OPERATIONTYPE" layout="${event-context:item=OPERATIONTYPE}" />
          <parameter name="LOGGER" layout="${event-context:item=LOGGER}" />
          <parameter name="LOGMESSAGE" layout="${event-context:item=LOGMESSAGE}" />
          <parameter name="IP" layout="${event-context:item=IP}" />
          <parameter name="TIME_STAMP" layout="${longdate}" />
          <parameter name="USERNAME" layout="${event-context:item=USERNAME}" />
          <parameter name="LOGLEVEL" layout="${level:uppercase=true}" />
        </target>
      </targets>
      <rules>
        <logger name="*" minlevel="Trace"  writeTo="database" />
      </rules>
    </nlog>

    4、简单封装

    public class Logger
        {
            #region 初始化
            /// <summary>
            /// 数据错误无法获取用户时使用
            /// </summary>
            public static string DefaultUser = "system";
            /// <summary>
            /// 默认地址
            /// </summary>
            public static string DefaultIP = "127.0.0.1";
            NLog.Logger _logger;
            private Logger(NLog.Logger logger)
            {
                _logger = logger;
            }
            public Logger(string name) : this(LogManager.GetLogger(name))
            { }
            public static Logger Default { get; private set; }
            static Logger()
            {
                Default = new Logger(LogManager.GetCurrentClassLogger());
            }
            #endregion
            #region Process日志写入
            /// <summary>
            /// 日志写入
            /// </summary>
            /// <param name="APPNAME">菜单大类</param>
            /// <param name="MODULENAME">二级菜单</param>
            /// <param name="PROCNAME">本级菜单</param>
            /// <param name="OPERATIONTYPE">操作类型</param>
            /// <param name="LOGMESSAGE">详细日志</param>
            /// <param name="IP">ip地址</param>
            /// <param name="USERNAME">用户名</param>
            /// <param name="LOGGER">LOGGER</param>
            public void Process(string APPNAME, string MODULENAME, string PROCNAME, OperationType OPERATIONTYPE, string LOGMESSAGE, string IP, string USERNAME, string LOGGER)
            {
                if (LOGMESSAGE.Length > 3000)
                {
                    LOGMESSAGE = LOGMESSAGE.Substring(0, 3000);
                }
                LogEventInfo lei = new LogEventInfo();
                lei.Properties["ID"] = Guid.NewGuid().ToString("D");
                lei.Properties["APPNAME"] = APPNAME;
                lei.Properties["MODULENAME"] = MODULENAME;
                lei.Properties["PROCNAME"] = PROCNAME;
                lei.Properties["OPERATIONTYPE"] = OPERATIONTYPE;
                lei.Properties["LOGMESSAGE"] = LOGMESSAGE;
                lei.Properties["IP"] = IP;
                lei.Properties["USERNAME"] = USERNAME;
                lei.Properties["LOGGER"] = LOGGER;
                lei.Level = LogLevel.Info;
                _logger.Log(lei);
            }
            /// <summary>
            /// 日志写入,自己传日志类别
            /// </summary>
            /// <param name="APPNAME">菜单大类</param>
            /// <param name="MODULENAME">二级菜单</param>
            /// <param name="PROCNAME">本级菜单)</param>
            /// <param name="OPERATIONTYPE">操作类型</param>
            /// <param name="LOGMESSAGE">详细日志</param>
            /// <param name="IP">ip地址</param>
            /// <param name="USERNAME">用户名</param>
            /// <param name="LOGGER">LOGGER</param>
            /// <param name="Level">日志级别</param>
            public void Process(string APPNAME, string MODULENAME, string PROCNAME, OperationType OPERATIONTYPE, string LOGMESSAGE, string IP, string USERNAME, string LOGGER, LogLevel Level)
            {
                if (LOGMESSAGE.Length > 3000)
                {
                    LOGMESSAGE = LOGMESSAGE.Substring(0, 3000);
                }
                LogEventInfo lei = new LogEventInfo();
                lei.Properties["ID"] = Guid.NewGuid().ToString("D");
                lei.Properties["APPNAME"] = APPNAME;
                lei.Properties["MODULENAME"] = MODULENAME;
                lei.Properties["PROCNAME"] = PROCNAME;
                lei.Properties["OPERATIONTYPE"] = OPERATIONTYPE;
                lei.Properties["LOGMESSAGE"] = LOGMESSAGE;
                lei.Properties["IP"] = IP;
                lei.Properties["USERNAME"] = USERNAME;
                lei.Properties["LOGGER"] = LOGGER;
                lei.Level = Level;
                _logger.Log(lei);
            }
            #endregion      
        }
        /// <summary>
        /// 操作类型枚举
        /// </summary>
        public enum OperationType
        {
            /// <summary>
            /// 保存或添加
            /// </summary>
            [System.ComponentModel.Description("保存或添加")]
            ADD,
            /// <summary>
            /// 更新
            /// </summary>
            [System.ComponentModel.Description("更新")]
            UPDATE,
            /// <summary>
            /// 审核
            /// </summary>
            [System.ComponentModel.Description("审核")]
            AUDIT,
            /// <summary>
            /// 删除
            /// </summary>
            [System.ComponentModel.Description("删除")]
            DELETE,
            /// <summary>
            /// 读取/查询
            /// </summary>
            [System.ComponentModel.Description("读取/查询")]
            RETRIEVE,
            /// <summary>
            /// 登录
            /// </summary>
            [System.ComponentModel.Description("登录")]
            LOGIN,
            /// <summary>
            /// 查看
            /// </summary>
            [System.ComponentModel.Description("查看")]
            LOOK
        }

    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());
  • 相关阅读:
    .net实现支付宝在线支付
    彻头彻尾理解单例模式与多线程
    Linq中的Select与Select many
    MVC中子页面如何引用模板页中的jquery脚本
    浅谈MemCahe
    左连接,右连接,内连接(left join ,right join,inner join)
    协变与逆变
    子类对父类中的属性和字段的改写
    里氏转换
    MVC基础篇—控制器与视图数据的传递
  • 原文地址:https://www.cnblogs.com/hnsongbiao/p/8780965.html
Copyright © 2020-2023  润新知