I am trying to create a logging solution that involves multiple processes on multiple machines. I planned on using the UDPAppender to send all log messages to a single machine that would manage them. I have a few questions about patternstrings vs patternlayouts.
Because I need to know both which machine and which process that log message came from, I want to include that in the log as well. I found %property{log4net:HostName} for hostname, and that works great. However, I don't see anything for process id in PatternLayouts. I do, of course, see something like that in the PatternString. From the FAQ:
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="log-file-[%processid].txt" />
<layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" />
</appender>
But I am not sure if or how to mix and match the two (or even if this is the canonical way to do so).
So, my questions are:
What is the difference between PatternString and PatternLayout? Why have both?
I see the %processid in PatternString, how do I get the same in PatternLayout? Here is my test layout:
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] [%property{log4net:HostName}] %-5level %logger - %message%newline" />
</layout>
Finally, it makes sense to use the XML layout for the UDP appender. It looks like the XmlLayoutSchemaLog4j already adds the HostNameProperty to the XML message. If I wan't to add this new Process ID (and maybe Process Name) to the XML message, what is the best way to do this? Should I just copy src\Layouts\XmlLayoutSchemaLog4j.cs, modify it, and let log4net know that I created this new Layout (like the SampleLayoutsApp)?
Thanks for your help
You can add any properties you want to the GlobalContext. I use this context to store the process id, like this:
log4net.GlobalContext.Properties["pid"] = Process.GetCurrentProcess().Id;
Then you reference this property from your appender using a regular pattern, like this:
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %property{pid} %level %logger - %message%newline" />
</layout>
You can add as many properties as you want, but because of it's global nature, it works best for properties that don't change during your application's execution.