-1

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

Aragok
  • 303
  • 7
  • 16

1 Answers1

0

Use this Now

using log4net;
using log4net.Repository.Hierarchy;
using log4net.Core;
using log4net.Appender;
using log4net.Layout;

namespace Spectrum.Logging
{
    public class Logger
    {
        public static void Setup()
        {
            Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();

            PatternLayout patternLayout = new PatternLayout();
            patternLayout.ConversionPattern = "%date [%thread] %-5level %logger - %message%newline";
            patternLayout.ActivateOptions();

            RollingFileAppender roller = new RollingFileAppender();
            roller.AppendToFile = false;
            roller.File = @"Logs\EventLog.txt";
            roller.Layout = patternLayout;
            roller.MaxSizeRollBackups = 5;
            roller.MaximumFileSize = "1GB";
            roller.RollingStyle = RollingFileAppender.RollingMode.Size;
            roller.StaticLogFileName = true;            
            roller.ActivateOptions();
            hierarchy.Root.AddAppender(roller);

            MemoryAppender memory = new MemoryAppender();
            memory.ActivateOptions();
            hierarchy.Root.AddAppender(memory);

            hierarchy.Root.Level = Level.Info;
            hierarchy.Configured = true;
        }
    }
}

And then all I had to do was replace the code where I called the XML file with the following call:

//XmlConfigurator.Configure(new FileInfo("app.config")); // Not needed anymore
Logger.Setup();

Do this all and then try:-

For reference

Update :- Please refer this link Alternate solutions

Hitesh Anshani
  • 1,499
  • 9
  • 19