Recently i had an issue making Log4Net work (described here) but after that it was OK.
I have left this behind a while because i needed to develop some modules and i left the logging somewhat behind. Now that i look, i have even tried changing the name of the log file and the location (set it statically), it is creating it but not writing anything to it in both cases.
This is my log4Net config file:
<?xml version="1.0"?>
<configuration>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="console" />
<appender-ref ref="file" />
</root>
<appender name="console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger - %message%newline" />
</layout>
</appender>
<appender name="file" type="log4net.Appender.RollingFileAppender">
<file value="ApplicationLogging.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
</appender>
</log4net>
</configuration>
This is my Global.asax
[assembly: XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
XmlConfigurator.Configure();
ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
logger.Info("Application started.");
How i declare it:
readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
I than use it sometimes like this:
logger.Info("some logging here");
Or for Context logging i would use it like this:
context.Database.Log = (dbLog => logger.Debug(dbLog));
The file is created, but no content is written to it. Can anyone suggest me where or what to look for?
UPDATE: As suggested by stuartd i have added this in the web.config:
<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.txt" />
</listeners>
</trace>
</system.diagnostics>
Which writes the following content (pastebin)
Note that i have removed the first section which i didn't saw before and i think it is redundant?
<!--<appender name="console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger - %message%newline" />
</layout>
</appender>-->
Either way with or without it it doesn't work.
Here is the output WITH the first part NOT commented out (in its original state)
I tried the following as well: https://logging.apache.org/log4net/release/config-examples.html https://csharp.today/log4net-tutorial-great-library-for-logging/
I am clueless on why it can create it but not write to it... I can not seem to find anything on the web neither, all issues are regarding the creation of the file, not writing to it.
+1 for including the debug/trace log in your question.
The message log4net: Configuring Repository [log4net-default-repository]
appears multiple times in this log, which looks like the configuration is being set up twice, once via the XmlConfiguratorAttribute
and a second time via the call to XmlConfigurator.Configure();
.
Inspecting the source code of the XmlConfiguratorAttribute
reveals that it internally makes a similar call to XmlConfigurator.Configure(...);
private void ConfigureFromFile(ILoggerRepository targetRepository, FileInfo configFile)
{
if (m_configureAndWatch)
{
XmlConfigurator.ConfigureAndWatch(targetRepository, configFile);
}
else
{
XmlConfigurator.Configure(targetRepository, configFile);
}
}
This is what happens.
The XmlConfiguratorAttribute
initially sets up the loggers and appenders as defined in the log4net.config
file, and creates the empty ApplicationLogging.log
file.
Then the call to XmlConfigurator.Configure();
overwrites these loggers and appenders with the configuration as in web.config
(being the default location) which doesn't contain any as you are using a separate log4net.config
file.
Because of this, you end up without any Appender
, and nothing gets logged.
The solution is to apply XmlConfiguratorAttribute
or call XmlConfigurator.Configure()
with the path to the log4net.config
file (XmlConfigurator.Configure(new FileInfo("log4net.config"))
), but don't do both.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With