External Config Files with elmah - web-config

I am using elmah (v1.1.11517.0) and am trying to move the config to an external source.
My config currently looks like this:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="elmah">
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah"/>
<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
<section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
<section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah"/>
<section name="errorTweet" requirePermission="false" type="Elmah.ErrorTweetSectionHandler, Elmah"/>
</sectionGroup>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net configSource="Settings\RobDev\log4net.config" />
<elmah configSource="Settings\RobDev\ELMAH.config" />
</configuration>
log4net is happy and runs fine, however for elmah I get the error
Parser Error Message: The attribute 'configSource' cannot be specified because its name starts with the reserved prefix 'config' or 'lock'.
Which is a pain, the elmah file is definitely there, but something isn't happy.
What might be causing this?

The reason why you can't use the configSource element for elmah is because elmah is defined as a sectionGroup. You can use the configSource on Sections. That is why it works on log4net.
If you need the seperate config-file for web-deployment like Dan Atkinson you could do the following:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="elmah">
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah"/>
<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
<section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
<section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah"/>
<section name="errorTweet" requirePermission="false" type="Elmah.ErrorTweetSectionHandler, Elmah"/>
</sectionGroup>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net configSource="Settings\RobDev\log4net.config" />
<elmah>
<errorLog configSource="Settings\RobDev\errorLog.config" />
<errorMail configSource="Settings\RobDev\errorMail.config" />
<errorFilter configSource="Settings\RobDev\errorFilter.config" />
<errorTweet configSource="Settings\RobDev\errorTweet.config" />
<security configSource="Settings\RobDev\security.config" />
</elmah>
</configuration>
The downside is that you need a config file for each section. But you often do that for web deployment projects.

I've added a bounty to this question as I'd also like to know the answer to this.
I need it because I use Web Deployment functionality which replaces files by their configSource attribute.
In the meantime, you could move the contents of elmah.config into your web.config, replacing the <elmah configSource="..." />.

Related

EF 6.1 Configuration using Interceptors with WCF

I am trying to turn on database logging using the interceptor feature that was added in EF 6.1. It works fine when I turn on in a ASP.NET web.config file, but when I trying to use in a WCF service that uses EF 6.1, it gets the following error
Exception: System.ServiceModel.ServiceActivationException: The service '/DataAccessService.svc' cannot be activated due to an exception during compilation. The exception message is: The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception..The more telling message is from the inner exception: Unrecognized element 'interceptors'. web.config line 123)
NOTE: based on the first answer, I am providing more elements from the web.config
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
.
.
.
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
<interceptors>
<interceptor type="System.Data.Entity.Infrastructure.Interception.DatabaseLogger, EntityFramework">
<parameters>
<parameter value="C:\tmp\DataAccessLogOutput.txt" />
</parameters>
</interceptor>
</interceptors>
</entityFramework>
You didn't register the Entity Frame Work configuration section that defines <interceptors> correctly. In other words do you have this at the top of your config file?
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
</configuration>

How to tell if using EF5

Am taking a Silverlight / WCF RIA Service project from EF4 to EF5. Target is .NET4.5
Here is a spike - am using a DB First approach. ie create edmx, then use the Domain Services Classes Wizard
So looking good so far as have
EntityFramework (5.0.0.0)
Microsoft.ServiceModel.DomainServices.EntityFramework (4.0.0.0) DbDomainService in here
System.Data.Entity (4.0.0.0)
as opposed to old way of:
EntityFramework (5.0.0.0)
System.Data.Entity (4.0.0.0)
System.ServiceModel.DomainServices.EntityFramework (4.0.0.0) LinqToEntitiesDomainService in here
then have put a simple UI on to access the 2 table db and it worked
Problem: This web.config isn't what I expected:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="system.serviceModel">
<section name="domainServices" type="System.ServiceModel.DomainServices.Hosting.DomainServicesSection, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" allowDefinition="MachineToApplication" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web>
<httpModules>
<add name="DomainServiceModule" type="System.ServiceModel.DomainServices.Hosting.DomainServiceHttpModule, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</httpModules>
<compilation debug="true" targetFramework="4.5">
<assemblies>
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</assemblies>
</compilation>
<!-- profile stuff commented out-->
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true">
<add name="DomainServiceModule" preCondition="managedHandler" type="System.ServiceModel.DomainServices.Hosting.DomainServiceHttpModule, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</modules>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=aspnet-BusinessApplication5.Web-20130603151851;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="TestDBEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;initial catalog=TestDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
I was expecting to have to put in:
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Got these bits of config from the following. Perhaps they are all codefirst specific.
https://github.com/jeffhandley/riabooks
http://jeffhandley.com/archive/2012/12/10/RIA-Services-NuGet-Package-Updates-ndash-Including-Support-for-EntityFramework.aspx
http://mcasamento.blogspot.co.uk/2012/10/entity-framework-5-code-first-and-wcf.html
The RIA Services dll is compiled against EF 4, not EF5, so the binding Redirect is used to make everything work. If/when RIA Services is open sourced there will be a new release of RiaServices.EntityFramework that will be compiled against EF 5 and EF 6. One of the benefits of open source will be the ability to put out as many NuGet packages as I want to support all combinations.

Elmah ignoring ErrorFilter

I am trying to have elmah ignore 404 errors but they keep showing up. I am using asp.net mvc 3.
I have
<elmah>
<errorLog type="Elmah.SqlErrorLog, Elmah" logPath="~/App_Data" connectionStringName="ConnectionString" applicationName="/" />
<errorFilter>
<test>
<equal binding="HttpStatusCode" value="404" type="Int32" />
</test>
</errorFilter>
<security allowRemoteAccess="0" />
</elmah>
<sectionGroup name="elmah">
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />
<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
<section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
<section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" />
</sectionGroup>
Am I missing something? I used nuget to generate the web.config so I would think everything is in the web.config.
Possibly an issue with module order like in this post?
ELMAH - Filtering 404 Errors

Elmah webconfig error 500 on Window2008r2 MVC2 Project

Just found out that i don't need httpHandler and the httpHandlers in the system.web and it still running
I have tried to integrate Elmah to my MVC2 Project and it works fine on my local, But when I upload it to the Web, which is window2008-r2. There i get "500 - Internal server error."
When i remove the httpHandler and the httpHandlers from the webconfig the the error is gone but elmah is not running.
Please help, how you I get it running on the 2008RC?
Here is what in my webconfig file:
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah"/>
<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
<section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
<section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah"/>
<section name="errorTweet" requirePermission="false" type="Elmah.ErrorTweetSectionHandler, Elmah"/>
</sectionGroup>
...
...
<httpModules>
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
</httpModules>
<customErrors mode="RemoteOnly" defaultRedirect="/content/error.htm" />
...
...
<modules runAllManagedModulesForAllRequests="true">
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
</modules>
<handlers>
<add name="Elmah" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
<remove name="UrlRoutingHandler"/>
</handlers>
...
<security allowRemoteAccess="0" />
<!--
quickest log method
-->
<errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/Content/ErrorLog" />
...
I am going to guess you are using .net 4.0 on your 2008 box and this module is not compatible with .net 4
What I did is download the source files and compiled it as .net 4 and had no issues. I am currently running Elmah on server 2008 using .net 4.0 and all is working just fine.
Just as a reference here is an excerpt from my web.config file
<sectionGroup name="elmah">
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />
<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
<section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
<section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" />
</sectionGroup>
<elmah>
<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="ConnectionString" />
<security allowRemoteAccess="0" />
</elmah>
<location path="elmah.axd">
<system.web>
<authorization>
<deny users="?" />
</authorization>
<httpRuntime requestLengthDiskThreshold="256" maxRequestLength="2097151" />
</system.web>
</location>
<system.web>
<httpHandlers>
<add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
</httpHandlers>
<httpModules>
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
</httpModules>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true">
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
</modules>
<handlers>
<add name="Elmah" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
</handlers>
</system.webServer>
whether or not httpHandlers is required in system.web is determined by whether you are on IIS6....It is better you keep both pieces as shown in the example by odyth so that it works fine for both IIS 6 and 7

ELMAH on MongoDB web interface throw exception

after configure logs to MongoDB using ELMAH on MongoDB nuget package
it successfully log to MongoDB collection but when trying to access the Web interface it gives me the following exception:
Command 'create' failed: a collection 'collectionname' already exists (response: { "note" : "the autoIndexId option is deprecated and
will be removed in a future release", "ok" : 0.0, "errmsg" : "a
collection 'collectionname' already exists", "code" : 48, "codeName" :
"NamespaceExists" })
i don't know why each time it try to create the collection
the web config configuration for elmah:
<sectionGroup name="elmah">
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />
<section name="errorLog" requirePermission="false" type="Elmah.FallbackErrorLogSectionHandler, Elmah.FallbackErrorLog" />
<section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
<section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" />
<!--<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />-->
</sectionGroup>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules>
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />
</modules>
</system.webServer>
<elmah>
<security allowRemoteAccess="true" />
<errorLog type="Elmah.FallbackErrorLog, Elmah.FallbackErrorLog">
<add type="Elmah.MongoErrorLog, Elmah.MongoDB" connectionStringName="elmah-mongodb" />
<add type="Elmah.XmlFileErrorLog, Elmah" logPath="C:\MyApp\logs\ElmahFallback" />
</errorLog>
</elmah>
<location path="logs" inheritInChildApplications="false">
<system.web>
<httpHandlers>
<add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
</httpHandlers>
<authorization>
<!--<allow roles="*" />-->
<allow users="*" />
</authorization>
</system.web>
<system.webServer>
<handlers>
<add name="ELMAH" verb="POST,GET,HEAD" path="logs" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
</handlers>
</system.webServer>
</location>
this error is by MongoDB 3.2 and up decrepted function (related by indexes when you create a new collection), try with 3.0 mongos's version or change the mongo's driver. I'm using this package Install-Package elmah.mongodb in visual studio nuget package console and mongo 3.0 version in c#.net and it's running perfectly.
I hope that it help you!!! good luck.