Common.Logging config exception - c#

I'm getting the following exception when I try to call
var log = LogManager.GetLogger(this.GetType());
A first chance exception of type 'Common.Logging.ConfigurationException' occurred in Common.Logging.dll
An unhanded exception of type 'Common.Logging.ConfigurationException' occurred in Common.Logging.dll
Additional information: Failed obtaining configuration for Common.Logging from configuration section 'common/logging'.
This is a .NET 4 application with references to
log4net.dll
Common.Logging.dll
Common.Logging.log4net.dll
my app.config has the following:
<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<configSections>
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>
</configSections>
<common>
<logging>
<factoryAdapter type="Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter, Common.Logging">
<arg key="level" value="ALL" />
<arg key="showLogName" value="true" />
<arg key="showDataTime" value="true" />
<arg key="dateTimeFormat" value="yyyy/MM/dd HH:mm:ss:fff" />
</factoryAdapter>
</logging>
</common>
</configuration>
I'm trying to call like so:
var log = LogManager.GetLogger(this.GetType());
log.Debug(m => m("testing"));
What am I missing?

It runs ok if you remove element startup from your config.
EDIT: Instead of removing, just move the startup element after configsections.

Related

Facing issue while running feature, specflow in visual studio, build error

I have created a basic feature file and trying to execute my first program. I have installed all mandatory packages through NuGet.
Error received :
Error The "GenerateFeatureFileCodeBehindTask" task failed unexpectedly.
System.Exception: Error when reading project file. ---> System.Configuration.ConfigurationErrorsException: Unrecognized element 'unitTestProvider'.
at System.Configuration.ConfigurationElement.DeserializeElement(XmlReader reader, Boolean serializeCollectionKey)
at System.Configuration.ConfigurationSection.DeserializeSection(XmlReader reader)
at TechTalk.SpecFlow.Configuration.ConfigurationSectionHandler.CreateFromXml(String xmlContent)
at TechTalk.SpecFlow.Configuration.ConfigurationLoader.Load(SpecFlowConfiguration specFlowConfiguration, ISpecFlowConfigurationHolder specFlowConfigurationHolder)
at TechTalk.SpecFlow.Generator.Configuration.GeneratorConfigurationProvider.LoadConfiguration(SpecFlowConfiguration specFlowConfiguration, SpecFlowConfigurationHolder specFlowConfigurationHolder)
at TechTalk.SpecFlow.Generator.Configuration.GeneratorConfigurationProviderExtensions.LoadConfiguration(IGeneratorConfigurationProvider configurationProvider, SpecFlowConfigurationHolder configurationHolder)
at TechTalk.SpecFlow.Generator.Project.ProjectReader.ReadSpecFlowProject(String projectFilePath, String rootNamespace)
--- End of inner exception stack trace ---
at TechTalk.SpecFlow.Generator.Project.ProjectReader.ReadSpecFlowProject(String projectFilePath, String rootNamespace)
at TechTalk.SpecFlow.Generator.Project.MSBuildProjectReader.LoadSpecFlowProjectFromMsBuild(String projectFilePath, String rootNamespace)
at SpecFlow.Tools.MsBuild.Generation.SpecFlowProjectProvider.GetSpecFlowProject()
at SpecFlow.Tools.MsBuild.Generation.GenerateFeatureFileCodeBehindTaskExecutor.Execute()
at SpecFlow.Tools.MsBuild.Generation.GenerateFeatureFileCodeBehindTask.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext() specflowFramework
my app.config looks like:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<configSections>
<sectionGroup name="NUnit">
<section name="TestRunner" type="System.Configuration.NameValueSectionHandler" />
</sectionGroup>
<section name="specFlow" type="TechTalk.SpecFlow.Configuration.ConfigurationSectionHandler, TechTalk.SpecFlow" />
</configSections>
<specFlow>
<language feature="en-US" />
<unitTestProvider name="MSTest" />
</specFlow>
</configuration>
The unit test providers are not configured anymore in the app.config with SpecFlow 3. You have to use the appropriate NuGet package. Doc link: https://docs.specflow.org/projects/specflow/en/latest/Guides/UpgradeSpecFlow2To3.html#changes-to-how-unit-test-providers-are-configured
In your case, you have to add the SpecFlow.NUnit package to your project and adjust your app.config to
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<configSections>
<sectionGroup name="NUnit">
<section name="TestRunner" type="System.Configuration.NameValueSectionHandler" />
</sectionGroup>
<section name="specFlow" type="TechTalk.SpecFlow.Configuration.ConfigurationSectionHandler, TechTalk.SpecFlow" />
</configSections>
<specFlow>
<language feature="en-US" />
</specFlow>
</configuration>
The list of the correct NuGet package depending on your unit test runner is: https://docs.specflow.org/projects/specflow/en/latest/Installation/Unit-Test-Providers.html
Full disclosure: I am a developer and maintainer of SpecFlow and SpecFlow+

How to get contents of a .config file using ConfigurationManager.GetSection(columnsSectionName) without altering the sections' order?

In my config section I have the following:
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
<section name="NFeColumns" type="System.Configuration.DictionarySectionHandler" />
<section name="CTeColumns" type="System.Configuration.DictionarySectionHandler" />
</configSections>
<appSettings>
<add key="csv.separator" value=";" />
<add key="infNFe.columns" value="NFeColumns"/>
<add key="infCte.columns" value="CTeColumns"/>
<add key="infNFe.filename" value=".\Extracted\NFeCollection.csv"/>
<add key="infCte.filename" value=".\Extracted\CTeCollection.csv"/>
</appSettings>
<NFeColumns>
<add key="infNFe.Id" value="Id" />
<add key="ide.cUF" value="cUF" />
<add key="dest.CNPJ" value="CNPJ" />
<add key="dest.xNome" value="xNome" />
<add key="det.nItem" value="nItem" />
<add key="prod.cProd" value="cProd" />
<add key="prod.xProd" value="xProd" />
<add key="IPI.cEnq" value="cEnq" />
</NFeColumns>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
I want to catch the values from and put in a IDictionary in the same order as the configuration file. I`m doing this:
string columnsSectionName = ConfigurationManager.AppSettings[colFileName + ".columns"];
IDictionary columns = (IDictionary)ConfigurationManager.GetSection(columnsSectionName);
However, Im geting the <NFeColumns> in a totally different order, and I cant figure out why. Can you please help me?
ConfigurationManager.GetSection returns an instance of HashTable. Items in a hash table won't be stored in the order they are inserted. You will have to manually order them.

Split EnterpriseLibrary Block configurations into multiple files

Is it possible to split the configuration for an enterprise library block into multiple files?
For example: I have three assemblies and one hosting project. I want to store the entlib v6 Exception Handling Application Block (EHAB) configuration for each assembly in a separate config file located in the particular assembly.
The hosting project references the three assemblies:
assembly_X
ehabX.config
assembly_Y
ehabY.config
assembly_Z
ehabZ.config
Hosting project
using ehabX.config
using ehabY.config
using ehabZ.config
I already tried the following:
App.config in the hosting project:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common" requirePermission="true" />
</configSections>
<enterpriseLibrary.ConfigurationSource>
<sources>
<add name="ehabX" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common" filePath="ExceptionHandling\ehabX.config" />
<add name="ehabY" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common" filePath="ExceptionHandling\ehabY.config" />
<add name="ehabZ" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common" filePath="ExceptionHandling\ehabZ.config" />
</sources>
<redirectSections>
<add sourceName="ehabX" name="exceptionHandling" />
<add sourceName="ehabY" name="exceptionHandling" />
<add sourceName="ehabZ" name="exceptionHandling" />
</redirectSections>
</enterpriseLibrary.ConfigurationSource>
</configuration>
ehabX.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"/>
</configSections>
<exceptionHandling>
<exceptionPolicies>
<add name="Swallow NotImplementedException">
<exceptionTypes>
<add type="System.NotImplementedException, mscorlib" postHandlingAction="None" name="NotImplementedException"/>
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
</configuration>
ehabY.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"/>
</configSections>
<exceptionHandling>
<exceptionPolicies>
<add name="Swallow ArgumentException">
<exceptionTypes>
<add type="System.ArgumentException, mscorlib" postHandlingAction="None" name="NotImplementedException"/>
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
</configuration>
The ehabZ.config is omitted.
Using the EHAB with the policy "Swallow NotImplementedException" works fine. But if I try to use the policy "Swallow ArgumentException" defnied in ehabY.config I get this error message:
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionHandlingException: The policy with name 'Swallow ArgumentException' cannot be found. Exception handling aborted.
Any suggestions?
Unfortunately, there is nothing out of the box that will let you merge multiple configuration sources into one configuration set.
I think you can probably do what you want but you will have to do some coding. You'll need to read in the appropriate configuration sources and create a custom IConfigurationSource that will merge them all. Additive merge of different config sources? has an example of a MergeConfigurationSource that could help you.

Enterprise Pattern and Practise: configure Sql Azure transient fault Configuration

I am trying to implement SQL Azure Transient Fault Configuration in my ASP.NET application. I am trying to following: http://msdn.microsoft.com/en-us/library/hh680899(v=pandp.50).aspx
Though I didn't find the block on how can I configure this in config file. I am getting The type RetryManager does not have an accessible constructor. error in my code. I read in some forum that I should configure it using config file, but how is my question. Thanks.
EDIT: I am using Enterprise Lib 5.0
Here's a sample configuration:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="RetryPolicyConfiguration" type="Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.Configuration.RetryPolicyConfigurationSettings, Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling, Version=5.1.1209.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
<section name="typeRegistrationProvidersConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.TypeRegistrationProvidersConfigurationSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</configSections>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<RetryPolicyConfiguration defaultRetryStrategy="Incremental Retry Strategy" defaultSqlConnectionRetryStrategy="Backoff Retry Strategy" defaultSqlCommandRetryStrategy="Incremental Retry Strategy" defaultAzureStorageRetryStrategy="Incremental Retry Strategy" defaultAzureServiceBusRetryStrategy="Fixed Interval Retry Strategy">
<incremental name="Incremental Retry Strategy" retryIncrement="00:00:05" maxRetryCount="5" />
<fixedInterval name="Fixed Interval Retry Strategy" retryInterval="00:00:01" maxRetryCount="10" />
<exponentialBackoff name="Backoff Retry Strategy" minBackoff="00:00:01" maxBackoff="00:00:30" deltaBackoff="00:00:10" maxRetryCount="10" />
</RetryPolicyConfiguration>
<typeRegistrationProvidersConfiguration>
<clear />
<add name="Caching" sectionName="cachingConfiguration" />
<add name="Cryptography" sectionName="securityCryptographyConfiguration" />
<add name="Exception Handling" sectionName="exceptionHandling" />
<add name="Instrumentation" sectionName="instrumentationConfiguration" />
<add name="Logging" sectionName="loggingConfiguration" />
<add name="Policy Injection" sectionName="policyInjection" />
<add name="Security" sectionName="securityConfiguration" />
<add name="Data Access" providerType="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSyntheticConfigSettings, Microsoft.Practices.EnterpriseLibrary.Data" />
<add name="Validation" providerType="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationTypeRegistrationProvider, Microsoft.Practices.EnterpriseLibrary.Validation" />
<add sectionName="RetryPolicyConfiguration" name="RetryPolicyConfiguration" />
</typeRegistrationProvidersConfiguration>
<appSettings>
</appSettings>
</configuration>

Duplicate log entries with NServiceBus

I have duplicate entries in my log file for NServiceBus and I'm concerned this means I'm not starting it up correctly.
eg:
2012-03-21 10:44:37.9820|WARN|NServiceBus.Licensing|No valid license
file was found. The host will be limited to 1 worker thread.
2012-03-21 10:44:37.9820|WARN|NServiceBus.Licensing|No valid license
file was found. The host will be limited to 1 worker thread.
2012-03-21 10:44:38.0610|WARN|NServiceBus.Utils.MsmqUtilities|Queue
spy\private$\error does not exist.
2012-03-21 10:44:38.0610|WARN|NServiceBus.Utils.MsmqUtilities|Queue
spy\private$\error does not exist.
My config looks like this:
Configure.With()
.DefiningCommandsAs(t => typeof(ICommand).IsAssignableFrom(t))
.DefiningEventsAs(t => typeof(IEvent).IsAssignableFrom(t))
.DefiningMessagesAs(t => typeof(IMessage).IsAssignableFrom(t))
.Log4Net<NlogAppenderForLog4Net>(a => { })
.NinjectBuilder(Kernel)
.XmlSerializer()
.MsmqTransport()
.DefineEndpointName("subscriber.input")
.IsTransactional(true)
.PurgeOnStartup(false)
.MsmqSubscriptionStorage("subscriber")
.UnicastBus()
.LoadMessageHandlers()
.ImpersonateSender(false)
.CreateBus().Start(() => Configure.Instance.ForInstallationOn<NServiceBus.Installation.Environments.Windows>().Install());
and app.config:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="MsmqTransportConfig" type="NServiceBus.Config.MsmqTransportConfig, NServiceBus.Core"/>
<section name="UnicastBusConfig" type="NServiceBus.Config.UnicastBusConfig, NServiceBus.Core"/>
<section name="MessageForwardingInCaseOfFaultConfig" type="NServiceBus.Config.MessageForwardingInCaseOfFaultConfig, NServiceBus.Core" />
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" requirePermission="false" />
</configSections>
<MessageForwardingInCaseOfFaultConfig ErrorQueue="error"/>
<UnicastBusConfig>
<MessageEndpointMappings>
<add Messages="Publisher.Events" Endpoint="publisher.input" />
<add Messages="Publisher2.Events.Message, Publisher2.Events" Endpoint="publisher2.input" />
</MessageEndpointMappings>
</UnicastBusConfig>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<runtime>
</runtime>
</configuration>
The NServiceBus profiles configure their own logging by default. You need to implement IWantCustomLogging to tell NSB that you're setting up logging your self.
Sample:
http://docs.particular.net/samples/logging/hostcustom/

Categories