// Parses a layout parameter from the given text private static LayoutParameter ParseParameter(string text) { // Initialize the layout parameter with a temporary full name and text var parm = new LayoutParameter { Text = text, FullName = Regex.Match(text, ParameterNamePattern).ToString() }; // Figure the contingent flag and strip down the full name to remove the opening bracket and contingent flag parm.Contingent = parm.FullName[2] == '?'; parm.FullName = parm.Contingent ? parm.FullName.Substring(3) : parm.FullName.Substring(2); // Spllit out the full name into its parts var nameParts = parm.FullName.Split('.'); foreach (var namePart in nameParts) parm.NameList.Add(namePart); // Split out and clean up the parameter format parm.Format = Regex.Match(text, ParameterFormatPattern).ToString(); parm.Format = !String.IsNullOrEmpty(parm.Format) ? parm.Format.Substring(1) : null; parm.Format = !String.IsNullOrEmpty(parm.Format) && parm.Format.StartsWith("'") && parm.Format.EndsWith("'") ? parm.Format.Substring(1, parm.Format.Length - 2) : parm.Format; parm.Format = !String.IsNullOrEmpty(parm.Format) ? parm.Format.Replace("\\'", "'").Replace("\\\\", "\\") : parm.Format; return parm; }
// Formats special parameters in the log event, such as tags and exceptions private static string GetSpecialParameter(LayoutParameter parameter, LogEvent logEvent) { switch (parameter.FullName) { case "Tags": return logEvent.Tags != null && logEvent.Tags.Count > 0 ? String.Join(",", logEvent.Tags.ToArray()) : null; case "Exception": return FormatException(logEvent.Exception); } return null; }