9

In ELMAH for logging errors to the database you can write:

<errorLog type="Elmah.SqlErrorLog, Elmah"
            connectionStringName="EducoparkEntities"/>

However, if I use EntityFramework, this doesn't work because the connection string for EF contains metadata as well:

<add name="EducoparkEntities" connectionString="metadata=res://*/EducoparkData.csdl|res://*/EducoparkData.ssdl|res://*/EducoparkData.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=(Local);Initial Catalog=...;User Id=...;Password=...;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient"/>

So, how can I use the EntityFramework connection string in Elmah?

abatishchev
  • 98,240
  • 88
  • 296
  • 433

4 Answers4

10

1

You can extract the database connection string via the ConnectionStringBuilder provided in the entity framework.

private string ExtractConnectionStringFromEntityConnectionString(string entityConnectionString)
{
    // create a entity connection string from the input
    EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder(entityConnectionString);

    // read the db connectionstring
    return entityBuilder.ProviderConnectionString;
}

2

To plug that db connection string into Elmah you will have to set it on Application_Start ( in Global.asax)

Peter Gfader
  • 7,673
  • 8
  • 55
  • 56
  • 2
    A warning: if you need a **password** to access the database, it won't be included in the ProviderConnectionString. – Christian Genne Oct 12 '12 at 11:11
  • 2
    You have shown how to extract the connection string and where to perform the replacement, but you haven't shown *how* to replace Elmah's default connection string. How do I do that? – Jake Aug 12 '17 at 22:43
5
    public class YourErrorLog : SqlErrorLog
    {
        public override string ConnectionString
        {
            get
            {
                //return any Connection string EF or any
            }
        }
    }

and modify configuration

    <elmah>
        <errorLog type="YourAssembly.YourErrorLog, YourAssembly" connectionStringName="elmah-sqlserver" />
    </elmah>

Elmah will ask sql Connection string but when it need will get your connection string.

Nuri YILMAZ
  • 4,291
  • 5
  • 37
  • 43
  • I managed to run this, but had to create a proper constructor based on a constructor of SqlErrorLog (see http://www.yoda.arachsys.com/csharp/constructors.html -- Constructors are not inherited) – GôTô Apr 28 '15 at 15:12
  • @GôTô all we know ctor rules ;) attention please answer date. when i give this code SqlErrorLog doesn't have ctor with paramater, we talk about new elmah SqlErroLog has ctor parameter? – Nuri YILMAZ Apr 28 '15 at 21:39
2

You cannot - at least not directly. What you'd need to do is extract the part of the EF connection string that really references the database (the provider connection string), and put that into it's own entry in the <connectionStrings> section of your web.config:

<connectionStrings>
  <add name="EducoparkELMAH"
      connectionString="Data Source=(Local);Initial Catalog=...;User Id=...;Password=...;MultipleActiveResultSets=True" 
      provider="System.SqlClient" />
</connectionStrings>

Or you could do it programmatically - the entity context will have a property called "Connection", which in turn has a property "ConnectionString", which is the one you're looking for:

string elmahConnectionString = EducoparkEntities.Connection.ConnectionString;

Marc

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
0

You can use Elmah.Contrib.EntityFramework nuget package for this purpose.

(Disclaimer: I wrote it)

abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • Is there a way to specify different schema when in database first mode? – Santhos Jan 29 '15 at 19:06
  • @Santhos: Yes, one of the [ctors](https://github.com/abatishchev/Elmah.Contrib.EntityFramework/blob/master/Elmah.Contrib.EntityFramework/ElmahContext.cs#L21) of `ElmahContext` accepts the schema name. – abatishchev Jan 29 '15 at 19:10
  • I know, I checked the code, but can that be configured directly from web.config? – Santhos Jan 29 '15 at 19:12
  • 1
    @Santhos: No, I don't think so, SQL Server connection string doesn't allow to specify the scheme name, see http://stackoverflow.com/questions/3282665/possible-to-set-default-schema-from-connection-string – abatishchev Jan 29 '15 at 19:14
  • I like your solution, I just now have to think whether I really want the tables and procedures in my default schema and under the same user - according to that question, it seems that one would have to create a new user, assign it a different schema and then let elmah log under that user. – Santhos Jan 29 '15 at 19:15
  • @Santhos: as I understand, a user the application itself is running under should have access to the ELMAH table otherwise it won't be able to write to it. Having different users you'll have to have a separate logic in custom `Elmah.ErrorLog` which will pass the `Elmah.ErrorLog` object to a service running under the dedicated user. If you have just one user you don't mess up with it. I'd say it isn't worth, it's kind of over-engineering. – abatishchev Jan 29 '15 at 19:24
  • I have successfully create a new user with permissions only to execute the three elmah stored procedures and its default schema is set to my "elmah" schema and it works like charm. The only disadvantage is that I cannot use your library. – Santhos Jan 30 '15 at 13:41
  • @Santhos: Congrats with that! What issue do you with it? – abatishchev Jan 30 '15 at 17:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69912/discussion-between-santhos-and-abatishchev). – Santhos Jan 30 '15 at 17:26