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:
Below are the Logger properties:
What am i doing wrong?
From your screenshots, we can see that your logger is not being initialised with your configuration, because
IsDebug
isfalse
. One thing I notice from your screenshot, is you're trying to pass the path toWeb.config
directly to theConfigure()
method. I realise that may be an attempt to solve the problem, so you may have already tried my next suggestion, but callingConfigure()
in the manner you currently have won't work becauseWeb.config
is not published to yourbin\debug
folder. It will calledWeb.projectname.config
. Callingwith 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:
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:
OK so i got it to work following this tutorial: log4net-guide-dotnet-logging
[assembly: XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
Called it like this:
Thank you for helping me! Kind regards