0
votes

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?

1
You did not mention the wpf extension you installed. Perhaps this extension does also integrate log4net and overwriting your configuration?dsdel
My extension installation is .vsixGifty
You mean you are trying to create an extension and when your created extension is installed somwhere it does not provide log output?dsdel
yes exactly. Created extension when installed doesn't provide the log output.Gifty
Could you check this so post ?dsdel

1 Answers

0
votes

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 :)