Solved

accessing web.config key from BL and DAL layer?

Posted on 2014-11-19
5
280 Views
Last Modified: 2014-12-14
Hi

I have 3 layers:
WEB APP
BLL
DAL

I have the error log system path stored in web.config

I now want to use this app key in the BLL and DLL logic layers to write to log files when an error occurs
i.e. on catching exeption:

myNS.ErrorLog.clsError.RegisterErrorInLogFile(System.Configuration.ConfigurationManager.AppSettings["ErrorPath"].ToString(), "DAL_MyAccount.cs", "UpdateScheduledReportData()", ex.Message);

Open in new window



however, it error on
System.Configuration.ConfigurationManager.AppSettings["ErrorPath"]
as its in a different project

any ideas what code i need?
0
Comment
Question by:websss
5 Comments
 
LVL 14

Expert Comment

by:Tchuki
Comment Utility
In the traditional n-layer architecture, logging is a cross-cutting concern (i.e. it has no particular home as it is shared functionality) and is therefore in an assembly of its own (maybe with other cross-cutting concerns such as policy settings) which is then referenced by your other layers.

Your "logging" would have all the configuration settings it needs, the other layers would just call the functions in that layer and everything should play nicely.
0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
Alternatively you would give you Web layer a means to initialise the BLL from the config, rather than the BLL needing direct access to it.

What exception are you getting? If your BLL is a class library you will have to manually add a reference to the System.Configuration assembly.
0
 
LVL 18

Expert Comment

by:Richard Lee
Comment Utility
I have used a very nice approach when accessing web specific settings within the other layers of your application. If you are using an IoC container then great, if not, you definitely need to start using one within your application.

This example is detailed in the following article and simply maps a dictionary of key/value pairs to an interface. That interface instance will then be passed to your other layers through dependency injection.

http://blog.mirajavora.com/elegant-app-settings-factory/

The great things about the appSettings is that it is exposed in .NET code as a dictionary, hence it works perfectly in this situation.
0
 

Author Comment

by:websss
Comment Utility
Thanks

I've tried the system.configuration in the .cs file
now i'm getting the following
"Unreachable Code Detected" on the "MyNS" word

 catch (Exception ex)
            {
                throw ex;
                MyNS.ErrorLog.clsError.RegisterErrorInLogFile(System.Configuration.ConfigurationManager.AppSettings["ErrorPath"].ToString(), "DAL_MyAccount.cs", "UpdateScheduledReportData()", ex.Message);

            }

Open in new window


I've got the NS added to references  folder
MyNS.ErrorLog

and i'm also
using MyNS.ErrorLog;
at the top of the file!?
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
Comment Utility
You need to switch those two lines around.  If you re-throw the exception first then the case block exits and the second line will never execute:
catch (Exception ex)
{   
    // this line needs to go first
    MyNS.ErrorLog.clsError.RegisterErrorInLogFile(System.Configuration.ConfigurationManager.AppSettings["ErrorPath"].ToString(), "DAL_MyAccount.cs", "UpdateScheduledReportData()", ex.Message);
    throw ex;
}

Open in new window

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now