You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by Matt Emory <re...@gmail.com> on 2010/07/28 19:32:11 UTC

Using an Appender as a Template

How can I add an appender in code but use options that I've defined in a
config file?

Here's an example where I'm dynamically adding an appender.  But ultimately,
I would like to define these options in the config file and load those
options into the appender I create here:

 log4net.Repository.Hierarchy.Logger l =
LogManager.GetLogger(projectName).Logger as
log4net.Repository.Hierarchy.Logger

      log4net.Appender.RollingFileAppender appender = new
log4net.Appender.RollingFileAppender();
      appender.AppendToFile = true;
      appender.RollingStyle =
log4net.Appender.RollingFileAppender.RollingMode.Size;
 appender.CountDirection = 1;
      appender.MaximumFileSize = "5000KB";
 …
 l.AddAppender(appender);

RE: Using an Appender as a Template

Posted by Rob Richardson <Ro...@rad-con.com>.
Or, you can use ConfigureAndWatch(), and edit the XML file from inside
your code.

RobR

Re: Using an Appender as a Template

Posted by Matt Emory <re...@gmail.com>.
Thanks!  Wasn't sure if there was some way to do it like how Vivek describes
it or not.  But having another live logger to pull settings from will work
for me.

On Wed, Jul 28, 2010 at 2:42 PM, Vivek R. Rathod <vr...@appsecinc.com>wrote:

> Yes, but this requires you to have a live logger defined in the config file
> as  opposed to just having an Appender defined. And, in case you are working
> with the  RollingFileAppender, when log4net loads the logger, it will touch
> the file you specify, which you may not want.
> -Vivek
> ________________________________________
> From: Rob Richardson [Rob.Richardson@rad-con.com]
> Sent: Wednesday, July 28, 2010 2:08 PM
> To: Log4NET User
> Subject: RE: Using an Appender as a Template
>
> Couldn't you have a sample logger defined in your config file, open that
> in code, and then copy its settings into your dynamically created
> appender?
>
> log4net.Repository.Hierarchy.Logger l =
> LogManager.GetLogger(projectName).Logger as
> log4net.Repository.Hierarchy.Logger
> log4net.Appender.RollingFileAppender appender = new
> log4net.Appender.RollingFileAppender();
>
> Log4net.Repository.Hierarchy.Logger sample =
> LogManager.GetLogger("SampleLog");
> appender.AppendToFile = sample.Appender["SampleAppender"].AppendToFile;
>
> RobR, probably showing his ignorance
>

RE: Rotated file name control

Posted by Rob Richardson <Ro...@rad-con.com>.
Thank you! 


Robert D. Richardson
Product Engineer Software
 
RAD-CON, Inc.
TECHNOLOGY: Innovative & Proven
Phone : +1.440.871.5720 ... ext 123
Fax:  +1.440.871.2948
Website:  www.RAD-CON.com
E-mail:  rob.richardson@RAD-CON.com


-----Original Message-----
From: Lansdaal, Michael T [mailto:michael.t.lansdaal@boeing.com] 
Sent: Thursday, July 29, 2010 10:02 AM
To: log4net-user@logging.apache.org
Subject: RE: Rotated file name control

Rob - I use the RollingFileAppender and set the date pattern so that I
get a .log extension.

<appender name="RollingDebugLogFileAppender"
type="log4net.Appender.RollingFileAppender">
	<file value="TrendMasterCS" />
	<appendToFile value="true" />
	<rollingStyle value="Composite" />
	<staticLogFileName value="false" />
	<datePattern value=".yyyy-MM-dd.lo\g" />

Thanks, Mike 

-----Original Message-----
From: Rob Richardson [mailto:Rob.Richardson@rad-con.com]
Sent: Wednesday, July 28, 2010 12:57 PM
To: Log4NET User
Subject: Rotated file name control

Greetings!

I am using a rotating file log.  The current file is TrendMasterCS.log.
Yesterday's file is TrendMasterCS.log.2010-07-10.  Since the default
action for a file is based on its extension, I would prefer the rotated
file name to be TrendMasterCS.2010-07-10.log.  I didn't see a way to
control that.  Can it be controlled?

Thank you.

RobR


RE: Rotated file name control

Posted by "Lansdaal, Michael T" <mi...@boeing.com>.
Rob - I use the RollingFileAppender and set the date pattern so that I get a .log extension.

<appender name="RollingDebugLogFileAppender" type="log4net.Appender.RollingFileAppender">
	<file value="TrendMasterCS" />
	<appendToFile value="true" />
	<rollingStyle value="Composite" />
	<staticLogFileName value="false" />
	<datePattern value=".yyyy-MM-dd.lo\g" />

Thanks, Mike 

-----Original Message-----
From: Rob Richardson [mailto:Rob.Richardson@rad-con.com] 
Sent: Wednesday, July 28, 2010 12:57 PM
To: Log4NET User
Subject: Rotated file name control

Greetings!

I am using a rotating file log.  The current file is TrendMasterCS.log.
Yesterday's file is TrendMasterCS.log.2010-07-10.  Since the default action for a file is based on its extension, I would prefer the rotated file name to be TrendMasterCS.2010-07-10.log.  I didn't see a way to control that.  Can it be controlled?

Thank you.

RobR


RE: Rotated file name control

Posted by Radovan Raszka <ra...@hasam.cz>.
Well - but you can wait infinitely ... There is no new release for many years, although many problems were fixed. But to get fixed version, you must take current sources from SVN and compile it yourself (not easy, I wasn't able to compile for CF).

Why new versions are not released? Are there any plans to create new release in the _near_ future? Or we can only download 1.2.10 version forever?

Radovan Raszka

-----Původní zpráva-----
Od: Rob Richardson [mailto:Rob.Richardson@rad-con.com] 
Odesláno: 28. července 2010 22:18
Komu: Log4NET User
Předmět: RE: Rotated file name control

Rob,

Thanks for the information.  It's not a big deal.  I'll just wait for an official version that has that property working.

RobR 

RE: Rotated file name control

Posted by Rob Richardson <Ro...@rad-con.com>.
Rob,

Thanks for the information.  It's not a big deal.  I'll just wait for an
official version that has that property working.

RobR 

RE: Rotated file name control

Posted by Rob Prouse <Ro...@Ivara.com>.
Rob

There is a property PreserveLogFileNameExtension checked into the trunk, but I don't think it is in the shipped version of log4net and there is an issue reported against it, https://issues.apache.org/jira/browse/LOG4NET-203, RollingFileAppender does not roll more than one time when PreserveLogFileNameExtension is true. The original issue that added this feature is at https://issues.apache.org/jira/browse/LOG4NET-64.

To use it, you would add the following line to your appender

<preserveLogFileNameExtension value="true"/>

I have a compiled version of the current source at http://www.alteridem.net/2010/07/09/log4net-udpappender-with-ipv6-on-windows-vista-and-7/ that you can try if this feature is important to you, but I would suggest testing it before you deploy as it is not an official release and likely contains bugs.

Rob

-----Original Message-----
From: Rob Richardson [mailto:Rob.Richardson@rad-con.com] 
Sent: Wednesday, July 28, 2010 3:57 PM
To: Log4NET User
Subject: Rotated file name control

Greetings!

I am using a rotating file log.  The current file is TrendMasterCS.log.
Yesterday's file is TrendMasterCS.log.2010-07-10.  Since the default
action for a file is based on its extension, I would prefer the rotated
file name to be TrendMasterCS.2010-07-10.log.  I didn't see a way to
control that.  Can it be controlled?

Thank you.

RobR

Rotated file name control

Posted by Rob Richardson <Ro...@rad-con.com>.
Greetings!

I am using a rotating file log.  The current file is TrendMasterCS.log.
Yesterday's file is TrendMasterCS.log.2010-07-10.  Since the default
action for a file is based on its extension, I would prefer the rotated
file name to be TrendMasterCS.2010-07-10.log.  I didn't see a way to
control that.  Can it be controlled?

Thank you.

RobR

RE: Using an Appender as a Template

Posted by "Vivek R. Rathod" <vr...@appsecinc.com>.
Yes, but this requires you to have a live logger defined in the config file as  opposed to just having an Appender defined. And, in case you are working with the  RollingFileAppender, when log4net loads the logger, it will touch the file you specify, which you may not want.
-Vivek
________________________________________
From: Rob Richardson [Rob.Richardson@rad-con.com]
Sent: Wednesday, July 28, 2010 2:08 PM
To: Log4NET User
Subject: RE: Using an Appender as a Template

Couldn't you have a sample logger defined in your config file, open that
in code, and then copy its settings into your dynamically created
appender?

log4net.Repository.Hierarchy.Logger l =
LogManager.GetLogger(projectName).Logger as
log4net.Repository.Hierarchy.Logger
log4net.Appender.RollingFileAppender appender = new
log4net.Appender.RollingFileAppender();

Log4net.Repository.Hierarchy.Logger sample =
LogManager.GetLogger("SampleLog");
appender.AppendToFile = sample.Appender["SampleAppender"].AppendToFile;

RobR, probably showing his ignorance

RE: Using an Appender as a Template

Posted by Rob Richardson <Ro...@rad-con.com>.
Couldn't you have a sample logger defined in your config file, open that
in code, and then copy its settings into your dynamically created
appender? 

log4net.Repository.Hierarchy.Logger l =
LogManager.GetLogger(projectName).Logger as
log4net.Repository.Hierarchy.Logger
log4net.Appender.RollingFileAppender appender = new
log4net.Appender.RollingFileAppender();

Log4net.Repository.Hierarchy.Logger sample =
LogManager.GetLogger("SampleLog");
appender.AppendToFile = sample.Appender["SampleAppender"].AppendToFile;

RobR, probably showing his ignorance

RE: Using an Appender as a Template

Posted by "Vivek R. Rathod" <vr...@appsecinc.com>.
I second this. If possible, I would like to do this as well
-Vivek
________________________________________
From: Matt Emory [repairman2003@gmail.com]
Sent: Wednesday, July 28, 2010 1:32 PM
To: log4net-user@logging.apache.org
Subject: Using an Appender as a Template

How can I add an appender in code but use options that I've defined in a config file?

Here's an example where I'm dynamically adding an appender.  But ultimately, I would like to define these options in the config file and load those options into the appender I create here:

 log4net.Repository.Hierarchy.Logger l = LogManager.GetLogger(projectName).Logger as log4net.Repository.Hierarchy.Logger

      log4net.Appender.RollingFileAppender appender = new log4net.Appender.RollingFileAppender();
      appender.AppendToFile = true;
      appender.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Size;
 appender.CountDirection = 1;
      appender.MaximumFileSize = "5000KB";
 …
 l.AddAppender(appender);