Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to set property [file] on object [log4net.Appender.FileAppender] #183

Closed
sc-mk opened this issue Sep 17, 2024 · 7 comments
Closed
Assignees
Milestone

Comments

@sc-mk
Copy link

sc-mk commented Sep 17, 2024

Hi,

We use the following in log4net.config for log4net 2.0.17:

<file type="log4net.Util.PatternString" value="Logs\file_%property{LogName}_%date{yyyyMMddHHmmss}.log" />

I updated log4net to latest version 3.0.0 via NuGet Manager in Visual Studio.
I got the following error:

Message: 
TestCleanup method Test.XYZ.TestCleanup threw exception. System.ArgumentException: System.ArgumentException: The parameter should not be null or empty.
Parameter name: fileName.

Stack Trace:
EqtAssert.StringNotNullOrEmpty(String parameter, String parameterName)
TestContextImplementation.AddResultFile(String fileName)
Logger.AttachLogFile(TestContext testContext) line 31
XYZ.TestCleanup() line 51

Standard Error: 
log4net:WARN Unable to set property [file] on object [log4net.Appender.FileAppender] using value [Logs\file_%property{LogName}_%date{yyyyMMddHHmmss}.log] (with acceptable conversion types)
log4net:WARN FileAppender: File option not set for appender [GeneralFileAppender].
log4net:WARN FileAppender: Are you using FileAppender instead of ConsoleAppender?

If I delete the type="log4net.Util.PatternString" from log4net.config, the log file will be created.
Otherwise no, it does not create a file.

Can you advise me what is wrong?

This worked for years until I upgraded to version 3.0.0

<configuration>
  <configSections>
    <section name="log4net" type="log4net. config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <log4net>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{ABSOLUTE} [%logger] %level - %message%newline%exception"/>
      </layout>
    </appender>
    <appender name="GeneralFileAppender" type="log4net.Appender.FileAppender">
      <file type="log4net.Util.PatternString" value="Logs\file_%property{LogName}_%date{yyyyMMddHHmmss}.Log"/>
      <appendToFile value="true"/>
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{ABSOLUTE} [%logger] %level - %message%newline%exception"/>
      </layout>
    </appender>
    <root>
      <level value="INFO"/>
      <appender-ref ref="GeneralFileAppender"/>
    </root>
  </log4net>
</configuration>
@ppkarwasz
Copy link
Contributor

Probably a typo in your config: the parameter name is fileName not file. Can you show us your configuration file?

@FreeAndNil
Copy link
Contributor

The property name is "file" in log4net, but it would be helpful to see the config file (the whole log4net section).

@geirg71
Copy link

geirg71 commented Sep 18, 2024

I also have this error after upgrading from log4net 2.0.17 to 3.0.0:

log4net:WARN Unable to set property [File] on object [log4net.Appender.RollingFileAppender] using value [mylog.log] (with acceptable conversion types)
log4net:ERROR Could not create Appender [RollingFileAppender] of type [log4net.Appender.RollingFileAppender]. Reported error follows.
System.NullReferenceException: Object reference not set to an instance of an object.
   at log4net.Appender.RollingFileAppender.ActivateOptions() in C:\log4net\src\log4net\Appender\RollingFileAppender.cs:line 993
   at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlElement appenderElement) in C:\log4net\src\log4net\Repository\Hierarchy\XmlHierarchyConfigurator.cs:line 334
log4net:ERROR Appender named [RollingFileAppender] not found.

As @mariankral said, if I remove type="log4net.Util.PatternString" from the config file the problem disappers.

image

An Internet search shows that there are many config files around using type="log4net.Util.PatternString" this way. Hence I guess this 3.0.0 change will probably break many configurations.

Random example (I have no connection to earthsoft):

https://help.earthsoft.com/ent_configure-log4net.htm

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <file type="log4net.Util.PatternString" value="%env{temp}/equislogs/{baseUri}/{process}.log" />
  <param name="AppendToFile" value="true" />
  <param name="MaxSizeRollBackups" value="10" />
  <param name="MaximumFileSize" value="500KB" />
  <param name="RollingStyle" value="Size" />
  <param name="StaticLogFileName" value="true" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%d %property{url} %property{ip} [%t] %-5p %c [%x] - %m%n" />
  </layout>
</appender>

@sc-mk
Copy link
Author

sc-mk commented Sep 18, 2024

@ppkarwasz @FreeAndNil
I added the whole config file.
I also tried with fileName, but result was the same error.

@FreeAndNil
Copy link
Contributor

@sc-mk, @geirg71 I can reproduce the issue in an unit test and verified that the problem does not occur in 2.x
I'm working on a fix.

@FreeAndNil
Copy link
Contributor

@sc-mk, @geirg71 Thanks for reporting the problem.

I fixed the regression in #184

Can you please test with https://github.com/apache/logging-log4net/releases/tag/rc%2F3.0.1-preview.2 or https://www.nuget.org/packages/log4net/3.0.1-preview.2 and close the issue?

@geirg71
Copy link

geirg71 commented Sep 19, 2024

Thank you @FreeAndNil I tested with 3.0.1-preview.2, and the problem is solved in my environment. I leave the issue closing to @sc-mk.

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

No branches or pull requests

4 participants