log4net rolling file appender not working

log4net rollingfileappender
log4net config file
log4net custom appender
log4net rollingfileappender rollingstyle
log4net levels
log4net multiple loggers
log4net threshold
log4net not logging

I have following configuration:

<log4net>
    <root>
      <level value="All"/>
      <appender-ref ref="RollingFileAppender"/>
    </root>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="mylog.log"/>
      <appendToFile value="true"/>
      <preserveLogFileNameExtension value="true"/>
      <rollingStyle value="Composite"/>
      <datePattern value=".yyyyMMdd"/>
      <maximumFileSize value="5MB"/>
      <countDirection value="1"/>
      <maxSizeRollBackups value="-1"/>
      <staticLogFileName value="false"/>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%date [%thread] %-5level %logger - %message%newline"/>
      </layout>
    </appender>
  </log4net>

And the following code in global.asax:

Sub Application_Start()
        AreaRegistration.RegisterAllAreas()

        RegisterGlobalFilters(GlobalFilters.Filters)
        RegisterRoutes(RouteTable.Routes)

        BasicConfigurator.Configure();

        LogManager.GetLogger(typeof(Global_asax)).Error("Logger configured")


    End Sub

For the tag file in configuration above. I have tried C:\my folder\mylog.log as well as what is seen above mylog.log

However, I do not see this file generated anywhere and there are no exceptions.

Environment: VS 2013 ASP.Net VB. Running via Debug menu. When I run IISExpress is started by VS2013 and site runs. but no log is seen.

I have tried various combinations for the file tag but nothing works.

Can someone help please?


This used to work for me (I've switched to the lighter and better configurable NLog), log rolled daily, was written in a subfolder named "Logs" under the app's path.

  <appender name="RollingDebugAppender" type="log4net.Appender.RollingFileAppender">
    <file value="Logs\" />
    <datePattern value="yyyy-MM-dd'-FULL.log'" />
    <staticLogFileName value="false" />
    <appendToFile value="true" />
    <rollingStyle value="Composite" />
    <maxSizeRollBackups value="100" />
    <maximumFileSize value="5MB" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
  </appender>

Have you added the 'Watch Configuration' to your Service assembly? e.g. using XML configuration. Hide Copy Code. log4net.Config. log4net.GlobalContext.Properties ["LogPath"] = String.Concat(FilePrefix,"_Debug.log"); log4net.ILog filelogger = log4net.LogManager.GetLogger("File"); filelogger.Debug(info); Issue: Although the property named LogPath is getting set properly, i dont see any file with the name specified through the code getting created in the path specified.


This is final version of what I ended up doing that worked:

 <log4net>
    <root>
      <level value="ALL"/>
      <appender-ref ref="RollingFileAppender"/>
    </root>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="logs\log4net.log"/>
      <datePattern value="yyyy-MM-dd'-FULL.log'" />
      <appendToFile value="true"/>
      <preserveLogFileNameExtension value="true"/>
      <rollingStyle value="Size"/>      
      <maximumFileSize value="250KB"/>      
      <maxSizeRollBackups value="-1"/>
      <staticLogFileName value="false"/>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%date [%thread] %-5level %logger - %message%newline"/>
      </layout>
    </appender>
  </log4net>

And

XmlConfigurator.Configure()
LogManager.GetLogger(typeof(Global_asax)).Error("Logger configured")

This logged to log\log4net.log in the application directory (i.e. where my code is )

As you would like to use Composite style, you are missing couple of configuration entries in your config. You need to change your config like this (change values  If Date/Time based rolling is enabled then the appender will not roll the log file at the Date/Time boundary but at the point when the next message is logged after the boundary has been crossed. The RollingFileAppender extends the FileAppender and has the same behavior when opening the log file.


You should turn on log4net's internal debugging to figure out what's failing...

Sample web.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <appSettings>
    <add key="log4net.Internal.Debug" value="true"/>
  </appSettings>

  <system.diagnostics>
    <trace autoflush="true">
      <listeners>
        <add name="textWriterTraceListener"
             type="System.Diagnostics.TextWriterTraceListener"
             initializeData="C:\log4net_internal.log"/>
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

More information available here on the troubleshooting section.

If you have used log4net for your logging need, you may have come across the bufferSize property of the appender which will allow you to  Try adding <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> to your <appender /> element. There is some performance impact because this means that log4net will lock the file, write to it, and unlock it for each write operation (as opposed to the default behavior, which acquires and holds onto the lock for a long time).


*_%date{yyyyMMdd}.log

below is the key, if this is missing u will see logs like above

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <appSettings>
    <add key="log4net.Internal.Debug" value="true"/>
  </appSettings>

  <system.diagnostics>
    <trace autoflush="true">
      <listeners>
        <add name="textWriterTraceListener"
             type="System.Diagnostics.TextWriterTraceListener"
             initializeData="C:\log4net_internal.log"/>
 **<staticLogFileName value="false" />**
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

In case of problems with an application, it is helpful to enable logging so that the For example, in RollingFileAppender the public int MaxSizeRollBackups { set; }​  Log4Net appends the datepattern onto the file value, so you want the file extension on the former. – Craig Walker Oct 28 '11 at 5:04 Since I want to monitor a predictable log file name for the current day, I use for file value = logs/server.log then for datePattern value, I add a period before yyyy.


A maximum number of backup files when rolling on date/time boundaries is not supported. See Also. Reference. log4net.Appender Namespace. Apache log4net​  The problem is that a rolling file appender writes its entries sequentially. As we all know, an XML file has to start with opening a root element and end with closing the root element. Of course, we can always provide some kind of workaround. Log4netXmlLayout. As I mentioned before, we’re not going to write a custom appender.


This article is about troubleshooting and not a how-to. Earlier, a friend of mine was testing a ftp file upload application. One of his problems was a  Stack Exchange network consists of 176 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.


While they work great for a tutorial like this, I would Likewise, RollingFileAppender is a specialization of FileAppender which does rolling logs. If you are not familiar with the term rolling log, let me  The default rolling style is Composite. When set to Once this appender's AppendToFile property is set to false, otherwise the appender would append to a single file rather than rolling the file each time it is opened.