Exemplo n.º 1
0
 /// <summary>
 /// 自定义日志记录
 /// </summary>
 /// <param name="message"></param>
 /// <param name="type"></param>
 public static void Write(string message, string type = null, Exception ex = null)
 {
     try
     {
         type = type ?? "CustomAction";
         var isEntity = IsEntity.ContainsKey(type) ? IsEntity[type] : IsEntity[MessageType.Info.ToString()];
         var log      = LogManager.GetLogger(LoggerRepository.Name, type);
         if (isEntity)
         {
             var entity = new LogEntity()
             {
                 Exception = ex, Message = message, Type = MessageType.Custom, ThreadId = Thread.CurrentThread.ManagedThreadId, Time = DateTime.Now
             };
             message = JsonConvert.SerializeObject(entity);
             log.Logger.Log(null, new log4net.Core.Level(50000, type), message, null);
             return;
         }
         log.Logger.Log(null, new log4net.Core.Level(50000, type), message, ex);
     }
     catch (Exception e)
     {
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="message">日志消息</param>
        /// <param name="messageType">日志类型</param>
        /// <param name="ex">异常</param>
        /// <param name="type">配置类型</param>
        public static void Write(string message, MessageType messageType, Type type, Exception ex)
        {
            try
            {
                var  isEntity = IsEntity[messageType.ToString()];
                ILog log      = LogManager.GetLogger(LoggerRepository.Name, type);
                if (isEntity)
                {
                    var entity = new LogEntity()
                    {
                        Exception = ex, Message = message, Type = messageType, ThreadId = Thread.CurrentThread.ManagedThreadId, Time = DateTime.Now
                    };
                    message = JsonConvert.SerializeObject(entity);
                }

                switch (messageType)
                {
                case MessageType.Debug: if (log.IsDebugEnabled)
                    {
                        log.Debug(message);
                    }
                    break;

                case MessageType.Info: if (log.IsInfoEnabled)
                    {
                        log.Info(message);
                    }
                    break;

                case MessageType.Warn: if (log.IsWarnEnabled)
                    {
                        log.Warn(message);
                    }
                    break;

                case MessageType.Error:
                    if (isEntity)
                    {
                        log.Error(message);
                    }
                    else
                    {
                        log.Error(message, ex);
                    }

                    break;

                case MessageType.Fatal:
                    if (isEntity)
                    {
                        log.Fatal(message);
                    }
                    else
                    {
                        log.Fatal(message, ex);
                    }
                    break;
                }
            }
            catch (Exception e)
            {
            }
        }