Example #1
0
		private LoggingEvent CreateLoggingEvent(Level level, string message, string category, object data, Exception exception)
		{
			LoggingEventData eventData = new LoggingEventData();
			eventData.LoggerName = this.Logger.Name;
			eventData.Level = level;
			eventData.TimeStamp = DateTime.Now;

			if (exception != null)
			{
				if (String.IsNullOrEmpty(message))
				{
					eventData.Message = data == null ? exception.Message : exception.Message + "\r\n相关数据: " + XMS.Core.Formatter.PlainObjectFormatter.Simplified.Format(data);
					eventData.ExceptionString = exception.GetFriendlyStackTrace();
				}
				else
				{
					eventData.Message = data == null ? message : message + "\r\n相关数据: " + XMS.Core.Formatter.PlainObjectFormatter.Simplified.Format(data);
					eventData.ExceptionString = exception.GetFriendlyToString();
				}
			}
			else
			{
				eventData.Message = data == null ? message : message + "\r\n相关数据: " + XMS.Core.Formatter.PlainObjectFormatter.Simplified.Format(data);
			}

			LoggingEvent loggingEvent = new LoggingEvent(ThisDeclaringType, this.Logger.Repository, eventData, FixFlags.None);
			
			// LoggingEvent loggingEvent = new LoggingEvent(ThisDeclaringType, this.Logger.Repository, this.Logger.Name, level, message, exception);
			
			//// 应用相关的信息
			//loggingEvent.Properties["RunMode"] = RunContext.Current.RunMode.ToString().ToLower();
			//loggingEvent.Properties["AppName"] = Container.ConfigService.AppName;
			//loggingEvent.Properties["AppVersion"] = Container.ConfigService.AppVersion;

			// 日志类别
			loggingEvent.Properties["Category"] = String.IsNullOrEmpty(category) ? "default" : category;

			// 访问者信息
			loggingEvent.Properties["UserIP"] = SecurityContext.Current.UserIP;
			loggingEvent.Properties["UserId"] = SecurityContext.Current.User.Identity.UserId;
			loggingEvent.Properties["UserName"] = SecurityContext.Current.User.Identity.Name;


			return loggingEvent;
		}
Example #2
0
		/// <summary>
		/// 获取返回自定义错误编码 的 ReturnValue。
		/// </summary>
		/// <param name="code">错误编码</param>
		/// <param name="ex">异常</param>
		/// <param name="message">错误信息。</param>
		/// <returns>自定义错误编码的 ReturnValue。</returns>
		public static ReturnValue GetCustomError(int code, Exception ex, string message = "")
		{
			if (string.IsNullOrEmpty(message) && ex != null)
			{
				message = ex.GetFriendlyToString();
			}
			return new ReturnValue
			{
				Code = code,
				Message = message,
			};
		}