Windsor + LoggingFacility + log4net - how to set log4net.GlobalContext.Properties?

2,644 views
Skip to first unread message

Filip Kinsky

unread,
Oct 27, 2009, 7:30:44 AM10/27/09
to Castle Project Users
I'm using LoggingFacility for accessing ILogger in my application and
now I'd like to be able to set log4net properties. I need to be able
to define log file name programatically depending on parameters my
application is launched with. This should be set just once on
application startup and all the ILoggers supplied by container should
reflect it. So I defined my log4net.config file like this:

...
<appender name="rollingFile"
type="log4net.Appender.RollingFileAppender,log4net" >

<param name="File" value="%property{LogName}" />
...

and already tried to call log4net.GlobalContext.Properties["LogName"]
= "xxx.log" before registering windsor logging facility etc, but I'm
not able to get it working - I'm still getting logs generated into
"%property{LogName}" file. I was already looking into
ExtendedLog4netLogger.GlobalProperties and tried to use it, but it
also didn't work. I looked into its source code, but it seems that the
GlobalProperties property isn't used to set real
log4net.GlobalContext.Property at all?!

Did anyone succeeded in using log4net properties with Castle
LoggingFacility? I guess I could write my own ILoggerFactory/ILogger
implementation, but this would consume quite a lot of time for such
simple thing...

Filip Kinsky

unread,
Oct 27, 2009, 8:00:54 AM10/27/09
to Castle Project Users
strange thing is that this works when using LogName as a parameter in
log message pattern, but not for file name so I guess it's rather
log4net related problem and nothing windsor-specific...

log4net.GlobalContext.Properties["LogName"] = logName;
container.AddFacility("logging", new LoggingFacility
(LoggerImplementation.Log4net));
container.Resolve<ILogger>().Debug("test");

John Simons

unread,
Oct 27, 2009, 8:34:26 AM10/27/09
to castle-pro...@googlegroups.com
What you should do is create your own appender derived from RollingFileAppender

Cheers John

On 27/10/2009, at 11:00 PM, Filip Kinsky <fi...@filovo.net> wrote:

RollingFileAppender


Get more done like never before with Yahoo!7 Mail. Learn more.

Filip Kinsky

unread,
Oct 27, 2009, 11:39:05 AM10/27/09
to Castle Project Users
I just solve the problem fortunately. The only mistake I made is in
the log4net.config file - the definition of file name parameter of
appender should not be done using <param name="File" ...>, but it
should look like this:

<file type="log4net.Util.PatternString" value="%property{LogName}" />

It just started to work when I changed the log4net.config file like
this. Nasty behaviour ;(

On 27 říj, 13:34, John Simons <johnsimons...@yahoo.com.au> wrote:
> What you should do is create your own appender derived from RollingFileAppender
>
> Cheers John
>
> On 27/10/2009, at 11:00 PM, Filip Kinsky <fi...@filovo.net> wrote:
>
> RollingFileAppender
>
>       ___________________________________________________________________________ _______
> Get more done like never before with Yahoo!7 Mail.
> Learn more:http://au.overview.mail.yahoo.com/

Filip Kinský

unread,
Apr 29, 2013, 12:05:33 PM4/29/13
to OutOfTouch, castle-pro...@googlegroups.com
Hi, I'd don't remember it clearly, but I think it was just about correct RollingFileAppender configuration. Did you try to use the configuration element I mentioned: <file type="log4net.Util.PatternString" value="%property{LogName}" />  ?


On Mon, Apr 29, 2013 at 5:41 PM, OutOfTouch <outofto...@gmail.com> wrote:
Hi Filip,

I know this is an old thread but I need to do the same thing that you were trying to do here and I can't not find any examples of using the ExtendedLog4netLogger .cs class
Did you use this class to do this? Even if you didn't do you have any examples of using this class? All the examples I see are the fluent registration of the LoggingFacility
Anyone that knows feel free to answer please.

Thanks!

Krzysztof Kozmic

unread,
May 2, 2013, 6:01:41 PM5/2/13
to castle-pro...@googlegroups.com
there should be some tests that demonstrate that, no?

-- 
Krzysztof Kozmic

On Friday, 3 May 2013 at 2:20 AM, OutOfTouch wrote:

Do you or anyone else know how I can do this using the ExtendedLog4NetLogger.cs?


On Tuesday, October 27, 2009 10:39:05 AM UTC-5, Filip Kinsky wrote:

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-u...@googlegroups.com.
To post to this group, send email to castle-pro...@googlegroups.com.
Visit this group at http://groups.google.com/group/castle-project-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

OutOfTouch

unread,
May 3, 2013, 2:40:07 AM5/3/13
to castle-pro...@googlegroups.com
I think I found where these tests are at but I am still confused as to how to do the registration. I am looking at ExtendedLog4NetFacilityTestCase.cs


On Thursday, May 2, 2013 5:01:41 PM UTC-5, Krzysztof Koźmic wrote:
there should be some tests that demonstrate that, no?

-- 
Krzysztof Kozmic

On Friday, 3 May 2013 at 2:20 AM, OutOfTouch wrote:

Do you or anyone else know how I can do this using the ExtendedLog4NetLogger.cs?


On Tuesday, October 27, 2009 10:39:05 AM UTC-5, Filip Kinsky wrote:
I just solve the problem fortunately. The only mistake I made is in
the log4net.config file - the definition of file name parameter of
appender should not be done using <param name="File" ...>, but it
should look like this:

<file type="log4net.Util.PatternString" value="%property{LogName}" />

It just started to work when I changed the log4net.config file like
this. Nasty behaviour ;(

On 27 říj, 13:34, John Simons <johnsimons...@yahoo.com.au> wrote:
> What you should do is create your own appender derived from RollingFileAppender
>
> Cheers John
>
> On 27/10/2009, at 11:00 PM, Filip Kinsky <fi...@filovo.net> wrote:
>
> RollingFileAppender
>
>       ___________________________________________________________________________ _______
> Get more done like never before with Yahoo!7 Mail.
> Learn more:http://au.overview.mail.yahoo.com/

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-users+unsub...@googlegroups.com.

OutOfTouch

unread,
May 8, 2013, 9:34:19 AM5/8/13
to castle-pro...@googlegroups.com
Hi Krzysztof,

I know you are a busy guy, I tried what you suggested in your answer to my question on stack over flow http://stackoverflow.com/questions/16343145/how-to-use-extendedlog4netlogger-cs-to-set-logfile-path-for-appender-at-runtime
the problem is the log file is never created and I don't get any runtime exception. I am not sure if it is because configuration happens before the property is set or what I am stumped.


On Thursday, May 2, 2013 5:01:41 PM UTC-5, Krzysztof Koźmic wrote:
there should be some tests that demonstrate that, no?

-- 
Krzysztof Kozmic

On Friday, 3 May 2013 at 2:20 AM, OutOfTouch wrote:

Do you or anyone else know how I can do this using the ExtendedLog4NetLogger.cs?


On Tuesday, October 27, 2009 10:39:05 AM UTC-5, Filip Kinsky wrote:
I just solve the problem fortunately. The only mistake I made is in
the log4net.config file - the definition of file name parameter of
appender should not be done using <param name="File" ...>, but it
should look like this:

<file type="log4net.Util.PatternString" value="%property{LogName}" />

It just started to work when I changed the log4net.config file like
this. Nasty behaviour ;(

On 27 říj, 13:34, John Simons <johnsimons...@yahoo.com.au> wrote:
> What you should do is create your own appender derived from RollingFileAppender
>
> Cheers John
>
> On 27/10/2009, at 11:00 PM, Filip Kinsky <fi...@filovo.net> wrote:
>
> RollingFileAppender
>
>       ___________________________________________________________________________ _______
> Get more done like never before with Yahoo!7 Mail.
> Learn more:http://au.overview.mail.yahoo.com/

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-users+unsub...@googlegroups.com.

Krzysztof Kozmic

unread,
May 8, 2013, 5:09:19 PM5/8/13
to castle-pro...@googlegroups.com
does it work if you use log4net directly?

-- 
Krzysztof Kozmic

To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-u...@googlegroups.com.

OutOfTouch

unread,
May 8, 2013, 5:29:59 PM5/8/13
to castle-pro...@googlegroups.com
Yes it does work if I just use the config file and set the path there.

OutOfTouch

unread,
May 8, 2013, 5:31:26 PM5/8/13
to castle-pro...@googlegroups.com
I am wondering if it is not working because it reads the config file before setting the global variable?

Krzysztof Kozmic

unread,
May 8, 2013, 5:39:15 PM5/8/13
to castle-pro...@googlegroups.com
dunno,

try it with just l4n first in exactly the same order and see if it works or not

-- 
Krzysztof Kozmic

To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-u...@googlegroups.com.

OutOfTouch

unread,
May 8, 2013, 5:57:03 PM5/8/13
to castle-pro...@googlegroups.com
With log4net and the normal ILogger  it worked when doing this:

log4net.GlobalContext.Properties[propertyName] = propertyValue;
log4net.Appender.RollingFileAppender rFileAppender = new log4net.Appender.RollingFileAppender();
log4net.Config.BasicConfigurator.Configure(rFileAppender);

But I honestly don' see why I would have to use the last two lines of code when the config file already has the appenders defined, I am not sure how I would add new appender using the extended appender if I have to use those last two lines of code.

Krzysztof Kozmic

unread,
May 8, 2013, 5:58:43 PM5/8/13
to castle-pro...@googlegroups.com
with Windsor you rely on the config file. Can you make it work with l4n and config file?

-- 
Krzysztof Kozmic

To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-u...@googlegroups.com.

OutOfTouch

unread,
May 8, 2013, 6:11:51 PM5/8/13
to castle-pro...@googlegroups.com
I don't understand your question I had it working by using the Windsor ILogger(Not the extended logger) and using the  code I posted  above.

OutOfTouch

unread,
May 8, 2013, 6:16:03 PM5/8/13
to castle-pro...@googlegroups.com

I should add though that the code I posted that worked with the windsor ILogger was ran before actually adding  my logger installer to the container.

OutOfTouch

unread,
May 8, 2013, 6:17:54 PM5/8/13
to castle-pro...@googlegroups.com
Even with Flips code in this thread he added the global property before adding the logger to the container

Krzysztof Koźmic

unread,
May 8, 2013, 6:20:27 PM5/8/13
to Castle Project Users

Ok so you're saying what used to work with normal logger stopped working when you switched to extended?

Krzysztof Kozmic
sent from my phone

To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-u...@googlegroups.com.

OutOfTouch

unread,
May 8, 2013, 6:44:50 PM5/8/13
to castle-pro...@googlegroups.com
No that is not what I am saying, what I am saying is if I wanted to use log4net directly to add thehe global property the code I I just posted works when using the windsor ILogger, if you look at Filips code he also added the global property using log4net directly and did it before adding the Logger to the windsor container. I don't want to use Log4net directly, I want to get this to work using the IExtendedLogger., 
To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-users+unsubscrib...@googlegroups.com.

To post to this group, send email to castle-pro...@googlegroups.com.
Visit this group at http://groups.google.com/group/castle-project-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-users+unsubscrib...@googlegroups.com.

To post to this group, send email to castle-pro...@googlegroups.com.
Visit this group at http://groups.google.com/group/castle-project-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-users+unsubscrib...@googlegroups.com.

To post to this group, send email to castle-pro...@googlegroups.com.
Visit this group at http://groups.google.com/group/castle-project-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-users+unsubscrib...@googlegroups.com.

To post to this group, send email to castle-pro...@googlegroups.com.
Visit this group at http://groups.google.com/group/castle-project-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

OutOfTouch

unread,
May 9, 2013, 9:55:51 AM5/9/13
to castle-pro...@googlegroups.com
Well right now I am convinced that I can't add an appender file path at runtime without using Log4net directly which to me makes it seem a little pointless to use Castle's Logging Facility for the abstraction since I have to use Log4net directly to do this. I still like castle,  I am just bummed out that this appears to not be working
To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-users+unsubscrib...@googlegroups.com.

To post to this group, send email to castle-pro...@googlegroups.com.
Visit this group at http://groups.google.com/group/castle-project-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-users+unsubscrib...@googlegroups.com.

To post to this group, send email to castle-pro...@googlegroups.com.
Visit this group at http://groups.google.com/group/castle-project-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-users+unsubscrib...@googlegroups.com.

To post to this group, send email to castle-pro...@googlegroups.com.
Visit this group at http://groups.google.com/group/castle-project-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-users+unsubscrib...@googlegroups.com.

To post to this group, send email to castle-pro...@googlegroups.com.
Visit this group at http://groups.google.com/group/castle-project-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Krzysztof Kozmic

unread,
May 9, 2013, 5:22:52 PM5/9/13
to castle-pro...@googlegroups.com
I'm fairly certain it should be working.

I'll have a look into that over the weekend, unless somebody else wants to do it

-- 
Krzysztof Kozmic

To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-u...@googlegroups.com.

Krzysztof Kozmic

unread,
May 9, 2013, 7:51:11 PM5/9/13
to castle-pro...@googlegroups.com
OK, so I had a stab at it this morning.

It seems you're right. In this context the global property has to be set before configure is called (which by default happens when you add the facility.

You never said why you're doing it that way and at which point in your applications lifecycle you were expecting to set the property.

In most cases this (having to use log4net directly to set it) wouldn't be an issue since you'd be doing it in the composition root.

-- 
Krzysztof Kozmic

OutOfTouch

unread,
May 15, 2013, 9:57:36 AM5/15/13
to castle-pro...@googlegroups.com
So are you saying you feel it is ok to use log4net directly to configure this in the installer before the facility is added?

Thanks for looking at this btw, much appreciated.

Krzysztof Kozmic

unread,
May 15, 2013, 6:21:46 PM5/15/13
to castle-pro...@googlegroups.com
I imagine you'd do it in the application root not in the installer, no?

-- 
Krzysztof Kozmic

To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages