Skip to content

File lock error when using FileAppender with XmlConfigurator.ConfigureAndWatch (v3.3.1) #321

Description

@urs-hart

When using the XmlConfigurator's file-watching, like this:

var logRepository = LogManager.GetRepository(typeof(Program).Assembly);
XmlConfigurator.ConfigureAndWatch(logRepository, new FileInfo("log.config"));

with a simple FileAppender config, like this:

<log4net>
	<appender name="log.log" type="log4net.Appender.FileAppender">
		<file value=".\log.log" />
		<layout type="log4net.Layout.PatternLayout">
			<conversionPattern value="%date %message%newline" />
		</layout>
	</appender>
	<root>
		<level value="ALL" />
		<appender-ref ref="log.log" />
	</root>
</log4net>

When editing the config-file at run-time, you get:
log4net:ERROR [FileAppender] ErrorCode: GenericFailure. Unable to acquire lock on file ...\log.log. The process cannot access the file '...\log.log' because it is being used by another process.

Due the file-change, the appenders are getting re-configured.
v3.3.0 old appenders were stopped and removed before new appenders were created and added.
v3.3.1 new appenders are created before old appenders are stopped.
The old appender still has a lock on the file.
The new appender is created and calls ActivateOptions()->OpenFile().

Activity

  1. FreeAndNil commented on Sep 19, 2026

    @FreeAndNil
    Contributor

    Confirmed, and your analysis is right: #287 (3.3.1) made the configurator activate the new appenders before swapping them in, so the incoming FileAppender opens the file while the outgoing one still holds it.

    Fix is in #322. Could you build that branch and check it against your setup? That is the measurement we are missing. If building from source is awkward, say so and we will put up a preview package.

    Two things so nothing surprises you:

  2. self-assigned this
    on Sep 19, 2026
  3. added this to the 3.5.0 milestone on Sep 19, 2026
  4. urs-hart commented on Sep 21, 2026

    @urs-hart
    Author

    I've built the 322 branch locally and tested it with my solution.
    I can confirm that this fixes my error.

    Thank you very much.
    Looking forward for release 3.50.

  5. FreeAndNil commented on Sep 21, 2026

    @FreeAndNil
    Contributor

    @urs-hart thanks for the feedback. I'm positive we can publish 3.5.0 by the end of September.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions