views:

446

answers:

2

Hi All,

How do I get the Application Name from an SQL Server connection string in the web.config file.

I want to use to log escalated error messages from a web application into the Windows Event Log.

Maybe there is a better way of doing this, ie using the IIS/Web application name?

Thanks

Mark

+1  A: 

What does the connection string look like?

DbConnectionStringBuilder is good for parsing and inspecting connection-string values by key:

        DbConnectionStringBuilder db = new DbConnectionStringBuilder();
        db.ConnectionString = connectionString;
        Console.WriteLine(db["Application Name"]);

otherwise you can get various details from the http server variables.

Marc Gravell
Just what I needed thanks, and thanks for the quick reply!
Mark Redman
A: 

SqlConnectionStringBuilder is also useful if you are using SQL Server:

SqlConnectionStringBuilder sc = new SqlConnectionStringBuilder(connectionString);
string applicationName = sc.ApplicationName;
adrianbanks