user1968030
user1968030

Reputation:

Log4net doesn't write to file

I want add new log to file.this is my appender:

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="mylogfile.txt"/>
  <appendToFile value="true"/>
  <rollingStyle value="Size"/>
  <maxSizeRollBackups value="5"/>
  <maximumFileSize value="10MB"/>
  <staticLogFileName value="true"/>
  <filter type="log4net.Filter.StringMatchFilter">
    <stringToMatch value="test"/>
  </filter>
  <filter type="log4net.Filter.StringMatchFilter">
    <stringToMatch value="error"/>
  </filter>
  <filter type="log4net.Filter.DenyAllFilter"/>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/>
  </layout>
</appender>
<root>
  <level value="All"/>
  <appender-ref ref="RollingFileAppender"/>
</root>

and on my class I add

[assembly: XmlConfigurator(Watch = true)]

and I add access everyone for the file but: log4net doesn't write to file. Why?

Upvotes: 13

Views: 17931

Answers (4)

Ricardo Fran&#231;a
Ricardo Fran&#231;a

Reputation: 3003

You just need to call Configure:

log4net.Config.XmlConfigurator.Configure();

You can see more details here: Log4net does not write the log file

Upvotes: 5

David Castro
David Castro

Reputation: 1957

You should add this config section:

<configSections>
<section name="log4net"     type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>

to refer the log4net configuration.

Upvotes: 1

Nicholas Carey
Nicholas Carey

Reputation: 74187

Log4net fails silently when there's a problem. The design conceit is that no logging is preferable to taking down the application. To figure out what's wrong, turn on Log4net's internal debugging by adding this key to your [app/web].config file:

<appSettings>
  <add key="log4net.Internal.Debug" value="true"/>
</appSettings>

The debug messages will be written to the console or to the System.Diagnostics.Trace system. More details from Phill Haack at http://haacked.com/archive/2006/09/26/Log4Net_Troubleshooting.aspx/

There are any number of reasons Log4net might fail. Permissions problems on the log file directory, for starters (especially true for server processes, where your likely running under a restricted set of permissions for security).

Upvotes: 31

stuartd
stuartd

Reputation: 73243

You need to initialise the logging as the very first step in your app, and from the same assembly that you have the [assembly] tag:

From the docs:

Therefore if you use configuration attributes you must invoke log4net to allow it to read the attributes. A simple call to LogManager.GetLogger will cause the attributes on the calling assembly to be read and processed. Therefore it is imperative to make a logging call as early as possible during the application start-up, and certainly before any external assemblies have been loaded and invoked.

Add something like this in your start up code:

LogManager.GetLogger("Initialises logging from assembly attributes");

Upvotes: 1

Related Questions