0

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>
    <root>
      <level value="ALL" />
      <appender-ref ref="LogFileAppender" />
    </root>
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
      <file value="myapp.txt" />
      <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>
</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

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");
            }
        }

But logs are not writing to the file. It's working in Console application but not working for WPF. Am I missing something?

James Z
  • 12,209
  • 10
  • 24
  • 44
Gifty
  • 185
  • 1
  • 2
  • 16
  • Where are you creating an instance of your TestWindowControl? – mm8 Mar 19 '18 at 14:00
  • Havent created an instance yet. Passing file path to the appender file param is writing logs to the file. But when I install this extension and execute logs are not writing to the file. – Gifty Mar 20 '18 at 07:27
  • 1
    If you haven't created an instance the code in the constructor haven't executed... – mm8 Mar 20 '18 at 15:36
  • Created an instance in VSPackage.cs file -> "MenuItemCallback". Now logs are writing to the file when I run in local but in the server, it's not happening. – Gifty Mar 21 '18 at 05:36
  • Server? How is a UserControl related to a server? – mm8 Mar 21 '18 at 14:23

1 Answers1

0

Try to set the filter inside appender

<filter type="log4net.Filter.LevelRangeFilter">
  <levelMin value="WARN" />
  <levelMax value="ERROR" />
</filter>
Smits
  • 195
  • 1
  • 2
  • 12