67

I have this code and the config file below:

ILog log = LogManager.GetLogger(typeof(MyClass));
log.Debug("Testing");

TestProj directory is not created and if I create it, no TestLog.txt file, no log ... nothing.

Any idea?

Thanks,

The config file

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

<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="C:\\TestProj\\TestLog.txt" />
  <appendToFile value="true" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="10MB" />
  <staticLogFileName value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
  </layout>
</appender>

<root>
  <level value="DEBUG" />
  <appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
Philipp M
  • 1,877
  • 7
  • 27
  • 38
TheBoubou
  • 19,487
  • 54
  • 148
  • 236

5 Answers5

123

You need to call the Configurefunction of the XmlConfigurator

log4net.Config.XmlConfigurator.Configure();

Either call before your first loggin call or in your Global.asax like this:

protected void Application_Start(Object sender, EventArgs e) {
   log4net.Config.XmlConfigurator.Configure();
}
pb2q
  • 58,613
  • 19
  • 146
  • 147
shriek
  • 5,157
  • 2
  • 36
  • 42
  • OK that's work :) Now I have to configure the file to see in the log what I want – TheBoubou Apr 18 '12 at 07:11
  • 2
    If you are using a Web project you may have to do new item and add the Global.asax file, because it doesn't exist by default. You should also immediately log something. log4net.Config.XmlConfigurator.Configure(); log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); log.Debug("Setup in Application_Start."); – ggb667 Nov 19 '14 at 19:29
  • Does this solution imply (which I hope) that the logger is there for all sessions of this web application? It certainly seems to work that way for me, but is it a bad idea? e.g. Globals.cs contans public static ILog Logger { get; set; } = LogManager.GetLogger("LOGGER"); and it is referenced everywhere as Globals.Logger?.Debug(LabelMsg.Text); or through a variable in some class to shorten: private ILog Logger { get; set; } = Globals.Logger; – Allen Apr 04 '21 at 18:19
24

Another way to do this would be to add this line to the assembly info of the web application:

// Configure log4net using the .config file
[assembly: log4net.Config.XmlConfigurator(Watch = true)]

Similar to Shriek's.

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
keni
  • 1,730
  • 13
  • 19
  • 1
    This will only work in "normal" applications, but not with an web application. – shriek Apr 18 '12 at 07:37
  • 1
    @shriek: I am not sure you are right. This attribute is used to configure the XmlConfigurator without calling one of the Configure methods. It is correct that `Watch=True` don't have much value because a change in the web.config always causes a restart of the web application – hwcverwe Apr 18 '12 at 08:07
  • 1
    @hwcverwe: I just had this very same problem yesterday, the Attribute was there but the logger didn't do anything. Only after calling the `Configure` Method before the first call to the logger it worked. (This was for a WCF service) – shriek Apr 18 '12 at 11:29
  • 1
    @shriek: +1 for your solution because you are making sure log4net has called the Configure method before you start logging. The assembly attribute is in some situations not working properly. But still I have also WCF services which are logging properly so it is not a fact websites and WCF services cannot use the assembly attribute. – hwcverwe Apr 18 '12 at 12:28
  • I use the assembly attribute in a web application and it works. The key is to instantiate the logger from the same project that has the assembly attribute (i.e. in Global.asax for example). See "When should I log my first message" in [FAQ](https://logging.apache.org/log4net/release/faq.html) – Tim Partridge May 16 '14 at 15:11
  • Can you add an assembly attribute in a web application in MSVS 10? – ggb667 Nov 19 '14 at 19:33
11

1: Add the following line into the AssemblyInfo class

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

2: Make sure you don't use .Net Framework 4 Client Profile as Target Framework (I think this is OK on your side because otherwise it even wouldn't compile)

3: Make sure you log very early in your program. Otherwise, in some scenarios, it will not be initialized properly (read more on log4net FAQ).

So log something during application startup in the Global.asax

public class Global : System.Web.HttpApplication
{
    private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(Global));
    protected void Application_Start(object sender, EventArgs e)
    {
        Log.Info("Startup application.");
    }
}

4: Make sure you have permission to create files and folders on the given path (if the folder itself also doesn't exist)

5: The rest of your given information looks ok

Daniel
  • 3
  • 1
  • 3
hwcverwe
  • 5,287
  • 7
  • 35
  • 63
2

often this is due to missing permissions. The windows account the local IIS Application Pool is running with may not have the permission to write to the applications directory. You could create a directory somewhere, give everyone permission to write in it and point your log4net config to that directory. If then a log file is created there, you can modify the permissions for your desired log directory so that the app pool can write to it.

Another reason could be an uninitialized log4net. In a winforms app, you usually configure log4net upon application start. In a web app, you can do this either dynamically (in your logging component, check if you can create a specific Ilog logger using its name, if not -> call configure()) or again upon application start in global.asax.cs.

Dirk Trilsbeek
  • 5,873
  • 2
  • 25
  • 23
1

I also had the similar issue. Logs were not creating.

Please check logger attribute name should match with your LogManager.GetLogger("name")

<logger name="Mylog">
      <level value="All"></level>
      <appender-ref ref="RollingLogFileAppender" />
    </logger>



private static readonly ILog Log = LogManager.GetLogger("Mylog");
Shakeer Hussain
  • 2,230
  • 7
  • 29
  • 52