Dynamic connection string for SQL server in ELMAH

453 views
Skip to first unread message

Ashish Gupta

unread,
May 11, 2009, 7:53:55 AM5/11/09
to ELMAH, ashish...@wipro.com
Hello,

I need to get ELMAH working with my application and It should log the
data in SQL server for a single database( one client). Its all good
and its working like treat. However,I have multiple databases (one for
each of the client of the application) and so I would create those
ELMAH tables in those databases. However,one needs to hard-code the
connection string in the Web,config. I know I can modify the
web.config n runtime. However,I was looking for a solution in which I
could change the code to meet this requirement. Something like
assigning the "SqlErrorLog.ConnectionString" property. But that is
read-only and I could not see any reference of the SqlErrorLog class
anywhere in the latest code. Could anybody provide let me know if any
code change would make this possible and could provide me the
directions?

Thanks in advance for the help,
Ashish

Atif Aziz

unread,
May 11, 2009, 8:49:04 AM5/11/09
to el...@googlegroups.com
However,I was looking for a solution in which I
could change the code to meet this requirement.

For all special requirements, sub-class the log implementation and override as necessary.


Something like
assigning the "SqlErrorLog.ConnectionString" property. But that is
read-only and

You can sub-class SqlErrorLog and override the ConnectionString property to have it calculated dynamically based on whatever conditions you need to take into account.


I could not see any reference of the SqlErrorLog class
anywhere in the latest code.

None of the concrete log implementations are reference anywhere in code. This is a configuration-time decision.

- Atif

James_2JS

unread,
May 11, 2009, 8:56:25 AM5/11/09
to ELMAH
Hi Ashish,

I was wondering, why you feel the need to have different error logs
for each of your clients?

It sounds like they are all using the same application, therefore
aren't you going to get the same errors regardless of the user? In
which case, I personally would go for a shared database approach for
your error logging.

Furthermore, ELMAH will record the current user at the time of the
error, so you will be able to derive who the client actually is if you
really need to at any point!!

Hope this helps!

Cheers,

James

Ashish Gupta

unread,
May 12, 2009, 12:02:21 AM5/12/09
to ELMAH

Atif,

Thank you for the reply and the directions. Just a question. If I
override the ConnectionString property in my subclass,how do I assign/
change the same in my code.
The way I do that in my application is to have a connectionstring like
this:-

<add name="ClientConnectionString"
value="server=localhost;database=Ashish_{ClientName};uid=
{ClientName};pwd={Password}"/>

on runtime,I replace the {ClientName} string with the name of the
client (say "ABC") so that the database name becomes Ashish_ABC and
the uid becomes "ABC". I generate the password for "ABC" and replace
the {Password} with the same. Any directions on how I can do the same
when I have a connection string like:-

<add name="ELMAHClientConnectionString"
value="server=localhost;database=Ashish_{ClientName};uid=
{ClientName};pwd={Password}"/>

Thanks again for the help,
Ashish

Ashish Gupta

unread,
May 12, 2009, 12:05:23 AM5/12/09
to ELMAH
James,

Actually shared database is a good option. However,we keep all the
info for a client in its database and so is the case with the
exception messages.
Also,same user might have access to more than one client.

let me know if you have a question,
Thanks again,
Ashish


On May 11, 5:56 pm, James_2JS <jamesdrisc...@btinternet.com> wrote:

James_2JS

unread,
May 12, 2009, 4:41:49 AM5/12/09
to ELMAH
Hi Ashish,

> Actually shared database is a good option. However,we keep all the
> info for a client in its database and so is the case with the
> exception messages.

I'm still struggling to see what benefit you get from logging the
errors in the client database.

1) I wouldn't imagine that you would be displaying the errors to your
users, so it makes no difference to them.
2) By keeping all the logs together it would enable you to spot
recurrences of the same error much more quickly, rather than having to
aggregate them across several schemas.

Maybe you have some other aspects to your solution that necessitate
something different, but I personally would just keep it simple!!!

Cheers,

James

Ashish Gupta

unread,
May 12, 2009, 5:48:04 AM5/12/09
to ELMAH
James,


Thanks for the comment. One thing which I would like to include in
case of shared database approach is the name of the client in the
table. Is there a way to do that?

Thanks,
Ashish

James_2JS

unread,
May 12, 2009, 6:19:51 AM5/12/09
to ELMAH
Hi again!

> Thanks for the comment. One thing which I would like to include in
> case of shared database approach is the name of the client in the
> table. Is there a way to do that?

At the moment, there is no way to do this directly within ELMAH.
However, there are a few possibilities to explore.

1) ELMAH records everything in the querystring, as well as the form
variables. If you are using these to determine the client, then you
have possibilities there
2) The ErrorLogModule will raise an OnLogged event once an error has
been logged. You could then store a link between the Error's id and
the client in the database in a separate (or modified) table.

With either of these approaches, you could then use the
ErrorLogDataSourceAdapter to create your own custom viewer.

Atif Aziz

unread,
May 12, 2009, 8:17:47 AM5/12/09
to el...@googlegroups.com
If I override the ConnectionString property in my subclass,how do I assign/
change the same in my code.

I don't understand why this would be any different than how you're saying you're already doing it in code:


on runtime,I replace the {ClientName} string with the name of the

If you've overridden the property, you can plug in any logic to do token replacement before returning the value. You can do this during the module initialize or during the property getter.

Muhammad Akmal

unread,
May 16, 2019, 1:10:51 AM5/16/19
to ELMAH
Hi,

I am using Elmah.bootstrapper and I need to log the errors is separate databases. Is here a way to use my subclass with bootstrapper?

Atif Aziz

unread,
May 16, 2019, 7:13:59 PM5/16/19
to el...@googlegroups.com
There's nothing specific here for the Bootstrapper. You would use the same techniques as before for ELMAH. See the following thread if you're looking for information on how to do it dynamically then check out the following thread:





On Thu, May 16, 2019 at 7:10 AM Muhammad Akmal <akmal....@gmail.com> wrote:
Hi,

I am using Elmah.bootstrapper and I need to log the errors is separate databases. Is here a way to use my subclass with bootstrapper?

--
You received this message because you are subscribed to the Google Groups "ELMAH" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elmah+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elmah/b7515935-be90-42e9-aea7-49acc1d30ea6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages