示例#1
0
		public static string GetMessageInner(bool useJSON, Layout layout, LogEventInfo info)
		{
			if (!useJSON)
				return layout.Render(info);

			var logLine = new LogLine
				{
					TimeStampISO8601 = info.TimeStamp.ToUniversalTime().ToString("o", CultureInfo.InvariantCulture),
					Message = info.FormattedMessage,
					Level = info.Level.Name,
					Type = "amqp",
					Source = new Uri(string.Format("nlog://{0}/{1}", HostName, info.LoggerName))
				};

			logLine.AddField("exception", info.Exception);

			if (info.Properties.Count > 0 && info.Properties.ContainsKey("fields"))
				foreach (var kv in (IEnumerable<KeyValuePair<string, object>>) info.Properties["fields"])
					logLine.AddField(kv.Key, kv.Value);

			if (info.Properties.Count > 0 && info.Properties.ContainsKey("tags"))
				foreach (var tag in (IEnumerable<string>) info.Properties["tags"])
					logLine.AddTag(tag);

			logLine.EnsureADT();

			return JsonConvert.SerializeObject(logLine);
		}
		public static string GetMessageInner(bool useJSON, Layout layout, LogEventInfo info, IList<Field> fields)
		{
			if (!useJSON)
				return layout.Render(info);

			var logLine = new LogLine
				{
					TimeStampISO8601 = info.TimeStamp.ToUniversalTime().ToString("o", CultureInfo.InvariantCulture),
					Message = info.FormattedMessage,
					Level = info.Level.Name,
					Type = "amqp",
					Source = new Uri(string.Format("nlog://{0}/{1}", HostName, info.LoggerName))
				};

			logLine.AddField("exception", info.Exception);

			if (info.Properties.Count > 0 && info.Properties.ContainsKey("fields"))
				foreach (var kv in (IEnumerable<KeyValuePair<string, object>>) info.Properties["fields"])
					logLine.AddField(kv.Key, kv.Value);

			if (info.Properties.Count > 0 && info.Properties.ContainsKey("tags"))
				foreach (var tag in (IEnumerable<string>) info.Properties["tags"])
					logLine.AddTag(tag);

			foreach (var propertyPair in info.Properties)
			{
				var key = propertyPair.Key as string;
				if (key == null || key == "tags" || key == "fields")
					continue;
				
				logLine.AddField((string) propertyPair.Key, propertyPair.Value);
			}

			if (fields != null)
				foreach (Field field in fields)
					if (logLine.Fields == null || !logLine.Fields.Any(x => x.Key == field.Name))
						logLine.AddField(field.Name, field.Layout.Render(info));

			logLine.EnsureADT();

			return JsonConvert.SerializeObject(logLine);
		}