How do I set a connection string for Enterprise Library Logging in my code
-
18. dubna 2012 13:53
I followed the example in "Logging Applicatin Block Hands-on-lab for Enterprise Library" (5.0). This is working now. I am able to log to the event log and to the database.
My question is this ... The connection string for the logging is being picked up in the Web.config file. For my application we have a methode for getting the connection string from another config file. This process is used throughout our company. What I want to do is to set the configuration string in my code and not pick it up from the web.config file. I have scoured the internet and have not found an example of anyone doing this.
In the lab, this is the statment that gets the writer to write to the log.
Private writer As LogWriter = EnterpriseLibraryContainer.Current.GetInstance(Of LogWriter)()
How do I attach a connection string to the LogWriter in my code?
Všechny reakce
-
18. dubna 2012 18:47
I can suggest to alternative ways:
1. Ask your web.config to redirect to the config folder.
<connectionStrings configSource="c:\myfolder\company.config"></connectionStrings>
2. Create a hard link to the file (web.config to new location)
Resolving n Evolving in C# (http://jeanpaulva.com)
-
18. dubna 2012 19:09
Thanks Jean Paul,
The other config file is a different structure. What I need to do is to change the connection string with my code at run time. There any number of things that could cause me to use a different connection string (ie. what server its running on, etc.)
I already of the logic to read our config file and generate the connection string, I do that for our SQL reads all the time. I just don't know how to tie the connection string to the "LogWriter"
-
19. dubna 2012 8:34Moderátor
Hi Rnes,
Welcome to the MSDN Forum.
Based on Jean's suggestion, how about create a hard connection string for this log writer?
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
20. dubna 2012 1:40That will not work. I am required to have all my connection strings in a different place. We have a method for encrypting and decrypting and determining what connection string to use based upon environmental and other factors. Therefor I have to be able to change the connection string in my code and not have a static connection string in the web config.
-
20. dubna 2012 2:55
Hi Rnes,
If you take a look at "Enterprise Library Docs - Adding Application Code" it says this:
"If you know the connection string for the database you want to create, you can bypass the application's configuration information and use a constructor to directly create the Database object. Because the Database class is an abstract base class, you must construct one of its derived types. The derived Database type determines the ADO.NET data provider. For example, the SqlDatabase class uses the SqlClientFactory provider, the SqlCeDatabase class uses the SqlCeProviderFactory provider, and the OracleDatabase class uses the OracleClientFactory provider. It is your responsibility to construct the appropriate type of Database class for the connection string."
Regards, http://shwetamannjain.blogspot.com
- Upravený Shweta Jain 20. dubna 2012 2:56
- Navržen jako odpověď Shweta Jain 20. dubna 2012 2:56
-
20. dubna 2012 5:52Moderátor
That will not work. I am required to have all my connection strings in a different place. We have a method for encrypting and decrypting and determining what connection string to use based upon environmental and other factors. Therefor I have to be able to change the connection string in my code and not have a static connection string in the web config.
Hi Rnes,
Based on your description, it seems that you want to read a configuration file at some other place to get a connection string. If so,
You can also store all your connection strings in your configuration file. And choose a proper one according to the other factors.
In addition, you can also build a connection string at run time when you only determine which data source in run time or chose by users.
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
20. dubna 2012 11:57
Mike,
Yes that is exactly what I am doing now for you SQL access. I read an external file to get the information to build the connection string at run time. Similar to what Shweta Jain<abbr class="affil"></abbr> pointed out. My question is now that I have the connection string, how do I tell the logger to use it instead of whats in the app.config file?
-
21. dubna 2012 5:44Moderátor
Mike,
Yes that is exactly what I am doing now for you SQL access. I read an external file to get the information to build the connection string at run time. Similar to what Shweta Jain<abbr class="affil"></abbr> pointed out. My question is now that I have the connection string, how do I tell the logger to use it instead of whats in the app.config file?
Hi Rnes,
Do you want to store the log into a database by LogWriter? As far as I know, this class can write the log into local logs rather than a database.
If I have misunderstood anything, please feel free to let me know.
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
26. dubna 2012 15:59
Mike,
Thanks for your reply. I was pulled away for a bit on some other issues.
Yes, I am trying to log the data to a SQL database. The database could be different depending on what server the application is running on. We have a common configuration that we use that stores our connection strings. That is why I need to be able to override the connection string at run time.
- Upravený RNes 26. dubna 2012 16:09
-
27. dubna 2012 3:55Moderátor
Hi Rnes,
When you connect to a SQL server database, there should be a connection, so you can change the connection string before the connection.open.
Am I right?
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
27. dubna 2012 6:19
Yea using the ado net connectionsstrings in .Net is one of the main problems since version 2005. It seems impossible to get that to be done simple.
We get every time solutions which have with every version more complexity included, while it was so simple in Framework 1.x.
But try it in the forum for Ado.net managed providers, this forum is in fact for System.Net and everything related to that as base library.
http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/threads
Success
Cor- Navržen jako odpověď Mike FengMicrosoft Contingent Staff, Moderator 2. května 2012 3:37
- Označen jako odpověď Mike FengMicrosoft Contingent Staff, Moderator 8. května 2012 5:32
- Zrušeno označení jako odpověď RNes 9. května 2012 2:35
-
1. května 2012 17:17
I wish there was Mike. Right now all the data access is done via config file. I wish I could do it from my code.
Russ.
-
1. května 2012 17:18
Thanks Cor. I will try that.
Russ.
-
9. května 2012 21:43
If anyone is interested. This was resolved ...
- Označen jako odpověď RNes 9. května 2012 21:43