tags:

views:

66

answers:

5

(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?

+2  A: 

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).

Oded
Adding a reference just mean your assembly now depends on the referenced library, so it is copied to every place the assembly is used. However, your assembly does not _expose_ or _use_ any features of the referenced library, you will not be able to use it.
Oded
It's a good solutions , but it's actually creating a wrapper when I don't really need / want one.
yossale
Then you probably need to reference log4net in all the projects that need it. It makes the dependency clear, and is probably what you need anyways. Use `Globals` for shared code and functionality.
Oded
A: 

That system won't work. You'll have to add the log4net dll as a reference to all the projects. Or create proxy classes, which is much more work.

Shlomo
A: 

Read up on the GAC (Global Assembly Cache), this a central storage for DLLs that are shared across projects... thats where I put my log4net DLL. You can then simply add the reference to it in your .config file forevery project you need to use it in without adding the DLL to the projects themselves.

This is a good place to start: MSDN: Working with the Global Assembly Cache

BG100
+2  A: 

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.

Fiona Holder
+2  A: 

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.

TJB