I'm getting an ConfigurationErrorsException “Couldn't find constructor for class CustomListener”

StackOverflow https://stackoverflow.com/questions/8595845

  •  24-03-2021
  •  | 
  •  

Вопрос

This is related to custom a Systems.Diagnostics.TraceListener

<system.diagnostics>
    <sources>
        <source name="SomeTraceSourceName" 
            switchType="System.Diagnostics.SourceSwitch" 
            switchName="SomeSwitchName">
            <listeners>
                <clear />
                <add name="CustomListener"/>
            </listeners>
        </source>
    </sources>
    <sharedListeners>
        <add name="CustomListener"
            type="CustomListener, MyAssembly" 
            initializeData=""/>
    </sharedListeners>
    <switches>
        <add name="SomeSwitchName" value="4"  />
    </switches>
</system.diagnostics>

This doesn't happen with the default trace listener.

I found this MSDN post, but ultimately, it didn't prove helpful.

Это было полезно?

Решение

Found it-- it took a long time.

The key was this part:

<add name="CustomListener"
            type="CustomListener, MyAssembly" 
            initializeData=""/>

When the intializationData is a blank string, it will look for a constructor with no arguments. As soon as I added the value for initializeData then the framework found the constructor.

The error should have said "No constructor with 0 parameters, maybe you need to include some initializeData"

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top