C# 解决方案:使用一个“Globals”外部 dll 的项目?
(抱歉,这可能是一个微不足道的问题,我来自 Java 和 Maven,但仍然没有把注意力集中在 C# 依赖项上)
我想在我的所有项目中使用 log4net。由于我不想将 dll 添加到所有项目,因此我创建了一个“Globals”项目,在其中添加对 log4net.dll 的引用,并从所有其他项目引用“Globals”项目。
但是,我似乎无法从任何其他项目访问 log4net 类。
using Globals.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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您所做的只是引用 DLL,那么您所做的就是获取 DLL 的一个副本,其中包含对您的
Globals
项目的每个引用。您仍然没有使用该库。我通常会创建一个 ILogger 接口,在 Globals 项目中使用 log4net 实现它,并在其他项目中使用该实现(加上用于测试的模拟实现)。
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 theGlobals
project and use that implementation in the other projects (plus a mock implementation for tests).恐怕事情不是这样的。
您必须将 DLL 添加到要从中调用它的所有项目中。
如果您只使用 DLL 中的几个函数,则可以在 Globals 项目中创建函数来调用它。
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.
log4net 并不只是通过引用“存在”在全局中。
我的第一个倾向是让所有项目都引用 log4net,它澄清了存在依赖关系,无需将其隐藏在另一个项目中。
但是,如果您确实在类之间共享通用逻辑,则可以有一个“全局”或“通用”类,其中包含对共享库的引用。要引用这些库,只需添加目标命名空间的使用。
换句话说,无论引用是在同一个项目中还是在另一个引用项目中,using 语句都是相同的。
对于 log4net,我认为它应该是:
添加正确引用的另一种方法是在代码中的某个位置键入类名之一(
Logger
?),然后使用“CTRL+”调用帮助器菜单”。或者通过简单地扩展它,可以选择添加正确的 using 语句。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:
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.那个系统行不通。您必须添加 log4net dll 作为对所有项目的引用。或者创建代理类,这需要更多工作。
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.
阅读 GAC(全局程序集缓存),这是跨项目共享的 DLL 的中央存储......这就是我放置 log4net DLL 的地方。然后,您只需在需要使用它的每个项目的 .config 文件中添加对它的引用,而无需将 DLL 添加到项目本身。
这是一个很好的起点:MSDN:使用全局程序集缓存
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