2

I have followed many different guides on how to configure the log4net, it is up and running but i can't find a log file anywhere ...

This is how my configuration look like:

Web.Config

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
<log4net debug="true">
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
        <file value="C:\\temp\\Log.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>

Global.asax:

XmlConfigurator.Configure(new FileInfo(Server.MapPath("~/Web.config")));
//XmlConfigurator.Configure();

StartUp.cs

//[assembly: XmlConfigurator(ConfigFile = "Web.config", Watch = true)]
[assembly: XmlConfigurator(Watch = true)]

Declaration

readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

Logging

 BasicConfigurator.Configure();
 logger.Info("Info logging ...");
 logger.Error("Homepage loading test logging ...");

Where my file value is: <file value="C:\\temp\\Log.txt" />

I have tried several paths, and commented out what above but no success.

What am i doing wrong?

UPDATE: As suggested by John H i have tried configuring and calling the logger in the Application_Start method and tried several alternative configs with it but with no luck. Here are 2 screenshots of some debugging info:

Main properties:

enter image description here

Below are the Logger properties:

enter image description here

What am i doing wrong?

Dimitri
  • 1,185
  • 2
  • 15
  • 37
  • 1
    Would you mind having a read of this https://stackoverflow.com/questions/51866569/log4net-not-logging-in-net-webapi-project-using-unitycontainer/51866721#51866721, and its question, to see if that solves your problem? They sound similar. – John H Aug 17 '18 at 17:58
  • I have tried provided suggestion in multiple scenario's (tried switching with code in comment above) but i can't find any log file anywhere on my PC (searched for it by name & extension as well) I will update original post with sceenshots from debugging on the logger object/instance itself if that is of any help. – Dimitri Aug 20 '18 at 06:44

2 Answers2

2

OK so i got it to work following this tutorial: log4net-guide-dotnet-logging

  • I have created a log4net.config file with content as showed in tutorial.
  • used [assembly: XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

Called it like this:

ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        logger.Info("Application started.");
  • file is created and content logged as well. I am gonna compare the config files content and see if the difference is in there and go gradually comparing everything till i have found what caused it not to work.

Thank you for helping me! Kind regards

Dimitri
  • 1,185
  • 2
  • 15
  • 37
1

From your screenshots, we can see that your logger is not being initialised with your configuration, because IsDebug is false. One thing I notice from your screenshot, is you're trying to pass the path to Web.config directly to the Configure() method. I realise that may be an attempt to solve the problem, so you may have already tried my next suggestion, but calling Configure() in the manner you currently have won't work because Web.config is not published to your bin\debug folder. It will called Web.projectname.config. Calling

XmlConfigurator.Configure()

with no parameters, will automatically resolve the correct configuration file in your output directory. I'm guessing you've tried that, but if that still doesn't work, try this as well:

using log4net;

protected void Application_Start(object sender, EventArgs e)
{
    // Initialising configuration before requesting a logger.
    XmlConfigurator.Configure();

    // Requesting a logger only after the configuration has been initialised.
    var logger = LogManager.GetLogger(typeof(Global));
    logger.Info("Application started.");
}

I'm not sure it will make any difference, but your configuration looks fine to me.

But by inspecting the IsDebug property on the logger, you'll at least be able to tell if the configuration has even been read.

Edit: One other thing, make sure the application will have the permissions to write to the file. From the documentation:

The RollingFileAppender extends the FileAppender and has the same behavior when opening the log file. The appender will first try to open the file for writing when ActivateOptions() is called. This will typically be during configuration. If the file cannot be opened for writing the appender will attempt to open the file again each time a message is logged to the appender. If the file cannot be opened for writing when a message is logged then the message will be discarded by this appender.

John H
  • 14,422
  • 4
  • 41
  • 74
  • thank you for your suggestion, i just tried it using the " XmlConfigurator.Configure();" instead of what i had, but 1 thing i can't seem to get is the use of Global, i can not reference it, i have tried using "MethodBase.GetCurrentMethod().DeclaringType" and "MvcApplication" or even "HttpApplication" but the "IsDebug" keeps being False... – Dimitri Aug 21 '18 at 15:08
  • 1
    @Dimitri If you look in your `Global.asax.cs`, it should be the name of the class that inherits from `HttpApplication` (e.g. `public class WebApiApplication : System.Web.HttpApplication`). Is that `MvcApplication` in your case? Also, in your second screenshot, can you check the list of `Appenders` to see what's in there? – John H Aug 21 '18 at 17:25
  • 1
    that is correct, it is indeed MvcApplication. I have set this type like this: LogManager.GetLogger(typeof(MvcApplication)); and i can see that the Appenders yielded no results, it is empty. I keep looking and trying for suggestions,thank you for your already given help John! – Dimitri Aug 22 '18 at 06:57