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
Solved

accessing web.config key from BL and DAL layer?

Posted on 2014-11-19
5
312 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: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 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

856 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