Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

accessing web.config key from BL and DAL layer?

Posted on 2014-11-19
5
Medium Priority
?
403 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 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

783 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