0

Log4net doesn't log when it is used inside a VSIX extension and installed at another target VS.

I have a WPF solution. I downloaded log4net dll, I added log4net.config and had set the "Copy to Output Directory" value as "Copy always".

log4net.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <log4net debug="true">
    <root>
      <level value="INFO" />
      <appender-ref ref="TestAppender" />
    </root>
    <appender name="TestAppender" type="log4net.Appender.FileAppender">
      <!--<file value="${TMP}\myapp.txt" />-->
      <!--<file value="c:\temp\logger.txt" />-->
      <!--<file value="${USERPROFILE}\log\Log.txt" />-->
      <!--<file value="${LOCALAPPDATA}\log\log.txt" />-->
      <file value="${ALLUSERSPROFILE}\Log.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="1KB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
      </layout>
    </appender>
  </log4net>
</configuration>

And I added the below line in AssemblyInfo.cs:

 [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

then the below code in my TestWindowControl.xaml.cs and referenced/created an instance for this in the VSPackage.cs.

public partial class TestWindowControl : UserControl
        {
            private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

            public TestWindowControl()
            {
                XmlConfigurator.Configure(new System.IO.FileInfo("log4net.config"));
                log.Info("info testing");
                log.Debug("debug testing");
                log.Error("error testing");
                log.Fatal("fatal testing");
                log.Warn("warn testing");
            }
        }

When I run this on my machine (local) it working as expected but I install this extension and try (Server) log4net is not logging with any of the location mentioned in log4net.config. Searched online and tried different paths but still, it's not logging. Am I missing something?

dsdel
  • 1,042
  • 1
  • 8
  • 11
Gifty
  • 185
  • 1
  • 2
  • 16
  • You did not mention the wpf extension you installed. Perhaps this extension does also integrate log4net and overwriting your configuration? – dsdel Mar 21 '18 at 09:02
  • My extension installation is .vsix – Gifty Mar 21 '18 at 09:15
  • You mean you are trying to create an extension and when your created extension is installed somwhere it does not provide log output? – dsdel Mar 21 '18 at 09:19
  • yes exactly. Created extension when installed doesn't provide the log output. – Gifty Mar 21 '18 at 09:24
  • Could you check this [so post](https://stackoverflow.com/questions/43262927/log4net-not-working-with-vs-2015-extension) ? – dsdel Mar 21 '18 at 09:27
  • Thank you dsdel for redirecting me to that post. XmlConfigurator.Configure(new System.IO.FileInfo("log4net.config")); this line has taken C:/windows/system32 path for log4net.config instead of the vsix extension installed path so I changed that line to XmlConfigurator.Configure(new FileInfo(Directory.GetParent(Assembly.GetExecutingAssembly().Location).FullName + @"\log4net.config")) & changed the log4net.config file properties BuildAction-Content and Include in VSIX-true now its working as expected. – Gifty Mar 22 '18 at 10:24

1 Answers1

0

After analyzation the problem was that log4net did not log when used inside a visual studio extension.

The solution was described in this so post

As written by Gifty in the comment:

Thank you. XmlConfigurator.Configure(new System.IO.FileInfo("log4net.config"));

This line was taking my local solution path for log4net instead of the extension installed path so I changed that line to var log4netconfigpath = Directory.GetParent(Assembly.GetExecutingAssembly().Location).FullName + @"\log4net.config";

and changed the log4net.config file properties BuildAction-Content and Include in VSIX-true now its working as expected. Thank you dsdel for redirecting me to that post :)

Community
  • 1
  • 1
dsdel
  • 1,042
  • 1
  • 8
  • 11