IC_
IC_

Reputation: 1789

log4net doesn't output anything

Here is my class:

internal class App : IDisposable
{
   private static readonly ILog _log = LogManager.GetLogger(typeof(App));

   internal void Start()
   {
            _log.Info("info");
            _log.Debug("debug");
            _log.Warn("warn");
            _log.Error("error");
            _log.Fatal("fatal");
            Console.WriteLine("test");
   }

   // ...
}

Main method:

public static void Main()
{
   var dir = AppDomain.CurrentDomain.BaseDirectory;
   XmlConfigurator.ConfigureAndWatch(new Hierarchy(), new FileInfo(Path.Combine(dir, "log4net.xml")));

   // ...
}

.csproj entry:

  <ItemGroup>
    <None Include="../*.env" CopyToOutputDirectory="Always" />
    <None Include="../log4net.xml" CopyToOutputDirectory="Always" />
  </ItemGroup>

log4net.xml:

<log4net xmlns="http://csharptest.net/downloads/schema/log4net.xsd">
  <appender name="console-default" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date %level %logger - %message%newline"/>
    </layout>
  </appender>

  <root>
    <level value="ALL"/>
    <appender-ref ref="console-default"/>
  </root>
</log4net>

_log object I get: log object debug

I also tried to add

using log4net.Config;
[assembly: XmlConfigurator(Watch = true)]

in Properties/AssemblyInfo.cs

Program output: app output

The same thing is if i replace appender with FileAppender it's just creating an empty file

log4net.xml file is copied each build into build directory near executable file
log4net version 2.0.8
config file is read correctly. If I change something it will throw error on initialization
enter image description here

Upvotes: 0

Views: 242

Answers (1)

pfx
pfx

Reputation: 23214

Pass an ILoggerRepository instance instead of a Hierarchy to the XmlConfigurator.ConfigureAndWatch.


var dir = AppDomain.CurrentDomain.BaseDirectory;
ILoggerRepository repository = log4net.LogManager.GetRepository(Assembly.GetCallingAssembly());
XmlConfigurator.ConfigureAndWatch(repository, new FileInfo(Path.Combine(dir, "log4net.config")));

There is no reason to have an xml namespace declaration on the configuration; use

<log4net>

instead of

<log4net mlns="http://csharptest.net/downloads/schema/log4net.xsd">

Upvotes: 1

Related Questions