Question

I have several websites that have separate ELMAH databases. I would like to have one separate website to view all these databases.

I've created a new ASP.NET MVC 4 website for this purpose. I've added the NuGet package "Elmah.MVC" and added the following to Web.config:

<connectionStrings>
    <add name="Elmah.Sql" connectionString="..." />
</connectionStrings>
<elmah>
    <errorLog type="Elmah.SqlErrorLog" connectionStringName="Elmah.Sql" />
</elmah>

This works just fine going to a single database. I can also switch database by changing the connectionstring, but now I want to be able to switch database from code. Is there a way to do that?

Was it helpful?

Solution

ELMAH has a class for customize the error log. You only need to implementing this class like

public class YourErrorLog : SqlErrorLog
{
    public override string ConnectionString
    {
        get
        {
            //return any connection string that you want
        }
    }
}

in this implementation, you can read any connection string that you want. Finally you can tell the ELMAH know about that ErrorLog by

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

You can see the details here

I also found out one example in your case here

Hope this help.

OTHER TIPS

I'm retrieving the connection string at runtime so the option from ThangChung wasn't working for me. But with a few minor changes i made it work.

The SqlErrorLog, both the constructors are required.

 public class ElmahSqlErrorLog : SqlErrorLog
 {
    public override string ConnectionString
    {
        get { return SettingsXmlService.GetConnectionString(); }
    }

    public ElmahSqlErrorLog(IDictionary config) : base(config)
    {
    }

    public ElmahSqlErrorLog(string connectionString) : base(connectionString)
    {
    }
 }

The web.config (add after system.webServer)

 <elmah>
    <errorLog type="Business.Infrastructure.Elmah.ElmahSqlErrorLog, Business" connectionString="-" applicationName="Topsite" />
 </elmah>

The value of the connectionString doesn't matter because we retrieve it at runtime, it can't be empty though.

If you want to switch the database from code, you won't be able to rely on the Web.config. You're going to need to store the active connection in a class like this:

public class DatabaseSession
{
    public string ConnectionString { get; set; }
}

and then you'll need to store an instance of that in Session:

Session["DatabaseSession"] = new DatabaseSession() { ConnectionString = "your connection string"; }

and you'll need to run that line every time you want to change the database connection. Then, when you want to connect to the database, you'll need to pull it out of session.

However, you could store all the different connection strings in the Web.config and let the user choose from that list -you just can't leverage it to determine the connection string.

NOTE: you could simply store the connection string in the session too Session["DatabaseConnection"] = "your connection string"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top