Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

accessing web.config key from BL and DAL layer?

Posted on 2014-11-19
5
Medium Priority
?
383 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 14

Expert Comment

by:Vel Eous
ID: 40452095
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
ID: 40452435
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
ID: 40455261
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
ID: 40461473
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 2000 total points
ID: 40461584
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

688 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