Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# solutions : using one "Globals" project for external dll's?

Tags:

c#

log4net

(Sorry for might be a trivial question , I'm coming from Java & Maven , and still haven't wrapped my mind around C# dependencies )

I want to use log4net in all my projects. Since I don't want to add the dll to all the projects , I've created a "Globals" project , add a reference to log4net.dll in it , and referenced from all the other projects to the "Globals" project .

However , I can't seem to access the log4net classes from any other project .

using Globals.log4net;

Doesn't seems to work either .

What am I doing wrong?

like image 846
Yossale Avatar asked Aug 22 '10 08:08

Yossale


3 Answers

If all you did was reference the DLL, then all you have done was get a copy of the DLL with every reference to your Globals project. You are still not using the library.

What I would normally do would create an ILogger interface, implement it using log4net in the Globals project and use that implementation in the other projects (plus a mock implementation for tests).

like image 100
Oded Avatar answered Sep 19 '22 06:09

Oded


I'm afraid that's not how it works. You have to add the DLL to all projects you want to call it from.

If you were only using a couple of functions in the DLL, you could create functions in your Globals project to call through to it.

like image 36
Fiona - myaccessible.website Avatar answered Sep 21 '22 06:09

Fiona - myaccessible.website


log4net doesn't 'live' in Globals simply by the reference.

My 1st inclination would be to have all of your projects just reference log4net, it clarifies that there's a dependency there no need to hide it in another project.

However, if you do have common logic shared across your classes you could have a "Global" or "Common" class which includes references to shared libraries. To reference those libraries just add the using of the target namespace.

In other words, no matter if the reference is within the same project or another reference project, the using statement will be the same.

For log4net i believe it should just be:

using log4net;

The other way to add the proper reference would be to type one of the class names somwhere in your code ( Logger ? ) and then invoke the helper menu with "CTRL+." or by simply expanding it, this will have the option to add the proper using statement.

like image 23
TJB Avatar answered Sep 22 '22 06:09

TJB