David Jacobsen
David Jacobsen

Reputation: 474

log4net use logger name in appender

I am trying to clean up the logging configuration one of our applications currently uses for it's log4net implementation. The application is called with an argument containing the filepath to an XML file. That XML file includes various configuration information, one of which is the logger name to use.

Currently, every configuration file has it's own logger, and every logger has their own appenders. As an example we would have:

and

Even though each of those appenders have nearly identical configurations. With hundreds of configuration files, this logging configuration section of the app.config is quite large. I believe I can consolidate most of these down to just a handful of generic appenders (Console, rollingFile, smtpToIT, smptpToSupport etc), and change each logger to use the generic appenders.

A big roadblock to how I visualize this working, is that each appender would need to use the logger's name in the configuration somewhere. Is there a variable or setting I can use in an appender that will allow the appender to use the logger's name? For instance, the RollingFileAppender should log to '\log[loggername].txt' The smtpAppender should have a subject of 'Log for [loggername] on MM\DD\YYYY'.

I took a look at http://logging.apache.org/log4net/release/config-examples.html, and believe I understand how the date can be added but I don't see anything about accessing the logger name from within the appender.

Is there anyway to access a property of the logger being used within the appender? Further, am I understanding how log4net is meant to be configured, with appenders being re-used among multiple loggers? I hadn't heard of log4net until a few weeks ago so I might be going about this all wrong.

Upvotes: 1

Views: 1795

Answers (2)

David Jacobsen
David Jacobsen

Reputation: 474

I found the solution elsewhere, in an answer to a different question. Unfortunately I don't remember where I found it and only came back to my original question because of Newtopian's answer.

You can set a Global property through the following line:

log4net.GlobalContext.Properties["id"] = "SomeText";

Then refer to it in the appender as I did below:

<appender name="NewAuto2ProcessingTestFILE" type="log4net.Appender.RollingFileAppender">
  <file type="log4net.Util.PatternString" value="Log/%property{id}NewAuto2ProcessingTest.txt" />
  <appendToFile value="true" />
  <maxSizeRollBackups value="10" />
  <staticLogFileName value="true" />
  <datePattern value=".yyyy-MM-dd.\\t\\x\\t" />
  <rollingStyle value="Date" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%d [%t] %-5p - %m%n" />
  </layout>
</appender>

Upvotes: 2

Newtopian
Newtopian

Reputation: 7712

Was looking for the same thing, unfortunately it seems it's not possible out of the box, check the doc here about the accepted variables in the pattern String.

You'd probably would have to create your own appender, or your own formatter, to gain access to more variables (I suspect both).

Upvotes: 1

Related Questions