Jeroen,
The FileAppender takes an exclusive file lock on the output file. What you
are seeing is a consequence of this.
You are making 2 requests to your aspx page. The first renders the page,
Page_Load is called (this is the first time Page_Load is called). The
FileAppender is created and takes a lock on the file. Log4net is configured
to use this appender and an Info message is generated and logged correctly.
The second request occurs when you click the AddAuthor button. This causes
the Page_Load method and then the btnAddAuthor_Click methods to run. The
Page_Load call creates a new FileAppender on the same file, this fails
because another FileAppender holds an exclusive lock on the file. Log4net is
then reconfigured to use this FileAppender instead of the previous appender.
The Info logs then succeed but the appender is unable to write them to the
log file.
It is usually better to configure log4net once during application startup
rather than during each Page_Load. Move the configuration code into the
Application_Start method in your Global.asax. It is more efficient to hold
on to your ILog object statically rather than looking it up for each
request. SO you could do something like:
private static ILog logger = LogManager.GetLogger("test");
Nicko
> -----Original Message-----
> From: Jeroen Wyseur [mailto:[EMAIL PROTECTED]
> Sent: 30 April 2004 14:13
> To: [email protected]
> Subject: BUG: Configuration is garbage collected
>
> Hi,
>
>
>
> When trying to log my web application I noticed the following:
>
> My FileAppender is garbage collected unless I keep a
> reference to it myself. Maybe this was intended to make sure
> I close the file appender after use?
>
>
>
> Example:
>
>
>
> public class WebForm1 : System.Web.UI.Page
>
> {
>
> private ILog logger;
>
>
>
> private void Page_Load(object sender, System.EventArgs e)
>
> {
>
> // Put user code to initialize the page here
>
> FileAppender appender = new
> log4net.Appender.FileAppender(new
> log4net.Layout.SimpleLayout(),"d:/data/temp/web.log",false);
>
> BasicConfigurator.Configure(appender);
>
> logger = LogManager.GetLogger("test");
>
> logger.Info("Log test");
>
>
>
> }
>
> private void btnAddAuthor_Click(object sender, System.EventArgs e)
>
> {
>
> Logger.Info("Author");
>
> }
>
>
>
> }
>
>
>
> The appender works when used in the Page_Load function but
> not in btnAddAuthor_Click (File could not be accessed). I
> solved this by adding a private variable FileAppender
> appender so I keep my own reference.
>
>
>
> Did I do something wrong? Where is in fact the best place to
> configure the logging for a web application?
>
> Kind Regards,
>
> Jeroen Wyseur
>
>
>
>
>
>