GitHub user martinRocks added a comment to the discussion: Support for template 
names

What do you think about doing something like this? I'm not sure if any of the 
names are something that my cloud log reader would ingest, but that should fix 
part of my problem.  To make this work, just add this class to the project and 
replace the Layout in the file appender.

```
public class MyLayout : LayoutSkeleton
    {
        public override void ActivateOptions()
        {
            
        }

        public override void Format(TextWriter writer, LoggingEvent 
loggingEvent)
        {
            var messageObject = loggingEvent.MessageObject;
            if (messageObject is string)
            {
                writer.WriteLine(messageObject);
                return;
            }
            var messageObjectFormat = 
((log4net.Util.SystemStringFormat)messageObject).Format;
            var templateNames = GetTemplateNames(messageObjectFormat);
            var args = ((log4net.Util.SystemStringFormat)messageObject).Args;

            var i = 0;
            var dic = new Dictionary<string, string>();
            foreach (var name in templateNames)
            {
                dic.Add(name, args[i].ToString());
                i++;
            }

            var serializer = new JsonSerializer();
            serializer.Serialize(writer, new 
TemplateWithValue(messageObjectFormat, dic));
            writer.WriteLine();
        }

        private static List<string> GetTemplateNames(string template)
        {
            var regex = new Regex(@"\{(\w+)\}");
            var matches = regex.Matches(template);

            return (from Match match in matches select 
match.Groups[1].Value).ToList();
        }

        private class TemplateWithValue
        {
            public TemplateWithValue(string template, Dictionary<string, 
string> values)
            {
                Template = template;
                Values = values;
            }
            public string Template { get; private set; }
            public Dictionary<string,string> Values { get; private set; }
        }
    }
```

GitHub link: 
https://github.com/apache/logging-log4net/discussions/261#discussioncomment-13621719

----
This is an automatically sent email for dev@logging.apache.org.
To unsubscribe, please send an email to: dev-unsubscr...@logging.apache.org

Reply via email to