0

I think I tried all possible combinations of calls to make log4net working woth ASP.NET Core 2.1 but no luck so far. I followed steps described in How to use log4net in Asp.net core 2.0 . I also explored all variants described in ASP.NET Core 2.1 + Log4Net.

No luck so far. The log4net.config is copied to the binary folder. It is used (I noticed it being locked when service is running). I used absolute and relative paths. Called and not called loggerFactory.AddLog4Net("log4net.config"); I called both:

var log4netConfig = new System.Xml.XmlDocument();
log4netConfig.Load(File.OpenRead(@"log4net.config"));
var repo = LogManager.CreateRepository(Assembly.GetEntryAssembly(), typeof(log4net.Repository.Hierarchy.Hierarchy));
XmlConfigurator.Configure(repo, log4netConfig["log4net"]);

and

ILoggerRepository repository = LogManager.GetRepository(Assembly.GetEntryAssembly());
XmlConfigurator.Configure(repository, new FileInfo("log4net.config"));

ILog is created in the following way:

private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

But also I used this approach:

var log2 = LogManager.GetLogger(typeof(Startup));
log2.Info("SOME");

Each time log does not have any Appenders created (list is empty) even though Configure method is called.

Of course no file is created. I noticed that some people reported that it can be added to your Core 2.1 project but I haven't discovered yet the right permutation of calls and actions.

Is there a way to trace what is actually log4net doing to be able to fix the problem? So far in my long experience log4net had been the most difficult component to work with. Or an exact combination of steps? I am using log4net 2.0.8 nuget.

Radek Strugalski
  • 560
  • 7
  • 22
  • Possible duplicate of https://stackoverflow.com/questions/756125/how-to-track-down-log4net-problems. – Polyfun Nov 21 '18 at 12:54
  • Not realy. The example there refers to a project which uses config in XML format. This is ASP.Net Core 2.1 which uses JSON to configure the web application. I don't how to translate that config file into a corresponding json. I am afraid that log4net won't be even looking into it as it cannot use JSON for configuration. – Radek Strugalski Nov 21 '18 at 14:44
  • Some of the posts in the thread I linked describe how to debug log4net without using a config file. – Polyfun Nov 21 '18 at 14:59

1 Answers1

0

Look's like there were 2 issues that added up to manifest the issus as I described above. Actually the log WAS created when at least following the explanation from the first link I provided. But, it was created in the wrong directory. The bug is explained here (.NET Core console app default working directory is the source directory, not the bin directory): https://github.com/dotnet/project-system/issues/589

Second issue was due to the fact that Logger has empty list of Appenders even though the appenders are loaded and used. This is apparently misleading but I didn't find any explanation to this behaviour. Seems like this is "by design".

So by default .NET Core Console Appliaction working directory is project root directory (sic!) no the bin directory as for .NET Framework case.

And secondly, there are no Appenders in the Logger, but this is not an indication of any problem.

Radek Strugalski
  • 560
  • 7
  • 22
  • Just to add. I had same exact issue after converting working code to core. Confusing b/c in conversion you're suspicious of things that might break anyway. Like you, I dropped in with debugger and thought there was a problem b/c no appenders. My problem turned out to be a different app.config (meant for other platform) which was erroneously included during conversion to modern csproj format. Was worried log4net wasn't ready for core, but works like a champ! – user1169420 May 01 '19 at 22:58