1

I have a VS2015 solution that includes a service layer, which exposes a set of WCF services, and a WPF client that consumes them. I have log4net configured and working fine in the service layer

I now want to use log4net in the client, so added the log4net NuGet package, and copied the node from the service layer's config file into the App.config file for the client. The full file looks like this...

<?xml version="1.0"
      encoding="utf-8"?>

<configuration>
  <startup>
    <supportedRuntime version="v4.0"
                      sku=".NETFramework,Version=v4.5.2" />
  </startup>

  <log4net>
    <appender name="RollingFileAppender"
              type="log4net.Appender.RollingFileAppender">
      <param name="File"
             value="PhysioDiary.log" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="2" />
      <maximumFileSize value="1MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-7level %logger - %message%newline%exception" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="RollingFileAppender" />
    </root>
  </log4net>

  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="LargeMessageBehavior">
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
        </behavior>
      </endpointBehaviors>
    </behaviors>

    <bindings>
      <customBinding>
        <binding name="CustomBindingDefault"
                 closeTimeout="00:59:00"
                 openTimeout="00:59:00"
                 receiveTimeout="00:59:00"
                 sendTimeout="00:59:00">
          <binaryMessageEncoding>
            <readerQuotas maxDepth="6400"
                          maxStringContentLength="100000000"
                          maxArrayLength="2147483647" />
          </binaryMessageEncoding>
          <httpTransport maxBufferPoolSize="2147483647"
                         maxReceivedMessageSize="2147483647"
                         bypassProxyOnLocal="true"
                         keepAliveEnabled="false"
                         maxBufferSize="2147483647">
            <extendedProtectionPolicy policyEnforcement="Never" />
          </httpTransport>
        </binding>
      </customBinding>
    </bindings>

    <client>
      <endpoint address="http://localhost:5448/AppointmentsService.svc"
                behaviorConfiguration="LargeMessageBehavior"
                binding="customBinding"
                bindingConfiguration="CustomBindingDefault"
                contract="AppointmentsServiceReference.AppointmentsService"
                name="CustomBinding_AppointmentsService" />
      <!-- Other WCF services here, snipped for clarity -->
    </client>
  </system.serviceModel>
</configuration>

Even before using log4net anywhere, if I try to run the client now, I get an exception...

An unhandled exception of type 'System.TypeInitializationException' occurred in PresentationFramework.dll

Additional information: The type initializer for 'System.Windows.Application' threw an exception.

...and the client won't start. It doesn't break on any code, so I can't see a stack trace, nor see if there is an inner exception.

If I comment out the contents of the node, but leave the empty node there, I still get the exception, so it doesn't seem to be the contents of the node, nor the RollingFileAppender that are the problem.

I've checked the references, and both projects have log4net v2.0.5 referenced, and neither have any other log4net references. Both projects use Ninject, although I'm not sure that's relevant as I'm not injecting log4net in either project (yet, that's the next step).

It's not the problem reported in this SO question, as my config file doesn't have appSettings nor configSections. Also, the startup node has always been the first in the file, and this has never caused a problem. I did try moving it to the end, but it didn't help.

Anyone any ideas why it crashes when I try to add log4net to the WPF client?

Community
  • 1
  • 1
Avrohom Yisroel
  • 8,555
  • 8
  • 50
  • 106

1 Answers1

4

I think it needs to register a type with config sections - something like : <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections>

Andrew
  • 2,598
  • 16
  • 27
  • Hmm, that worked, but I don't have that in the service config file. I can't understand why it works there and not here. However, I now have a different problem, and that is that the log file isn't being written. If I use the code from http://stackoverflow.com/a/1343913/706346 then it shows my Appenders collection as empty. As I said, I copied the node directly from the service project where it works fine. Thanks – Avrohom Yisroel Oct 05 '16 at 17:54
  • Hmm Part II. I found this post http://stackoverflow.com/a/8138647/706346 which suggested adding a line to Assembly.cs, which works fine. Again, I don't understand how the logging works in the service layer without this. – Avrohom Yisroel Oct 05 '16 at 17:58
  • Not really that familiar with log4net. As far as the service working without you needing to add anything else it is because .net already defines various sections and will do so for wcf. Not sure how the logging is working there, are the references really to log4net logging or something else maybe. – Andrew Oct 05 '16 at 18:02
  • Ho hum, it turned out that the service layer logging **wasn't** working, it just wasn't throwing any exceptions! I hadn't realised that the log file wasn't actually being written. Explains a few things eh? Thanks again for the help. – Avrohom Yisroel Oct 06 '16 at 15:56