Solved

accessing web.config key from BL and DAL layer?

Posted on 2014-11-19
5
324 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

Independent Software Vendors: 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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 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