i try to add log4net to my ASP.NET MVC 5 project, but it refuses to write to the log file. My log4net.xml file looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<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="C:/Users/Andreas/Documents/logs/myapp.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>
I load the config in my Startup.cs like this:
XmlConfigurator.Configure(new FileInfo(Path.Combine("log4net.xml")));
I include the logger in the classes i wanna log like this:
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
and my logging command:
log.Info("Hello logging world!");
I tried to activate the internal logging of log4net and either it doesnt throw any exceptions or it doesnt work, i don't get any log from log4net. Is there a better way to include log4net in project.json based projects? I have looked through similar topics on stackoverflow and other platforms but nothing helped.
Solution:
The tutorial under this link did the job: https://code.msdn.microsoft.com/How-to-use-Apache-log4net-0d969339