Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net process id information

Tags:

log4net

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:

  1. What is the difference between PatternString and PatternLayout? Why have both?

  2. 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> 
  3. 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

like image 511
teleball Avatar asked Jan 16 '10 00:01

teleball


People also ask

Is log4net using log4J?

The Apache log4net library is a tool to help the programmer output log statements to a variety of output targets. log4net is a port of the excellent Apache log4j™ framework to the Microsoft® .

How do I completely disable all logging at runtime?

How do I completely disable all logging at runtime? Setting the Threshold on the Hierarchy to Level OFF will disable all logging from that Hierarchy. This can be done in the log4net configuration file by setting the "threshold" attribute on the log4net configuration element to "OFF".

Is log4net thread safe?

Is log4net thread-safe? Yes, log4net is thread-safe. So, no need for manual locking.


1 Answers

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.

like image 170
jvilalta Avatar answered Oct 04 '22 07:10

jvilalta