Log4Net 和 GAC - 如何引用配置文件?

发布于 2024-08-29 18:25:50 字数 353 浏览 1 评论 0原文

我在开发过程中使用 log4net,作为项目约束的一部分,我现在需要将其添加到全局程序集缓存中。

日志记录定义位于文件 Log4Net.xml 中。该文件在我的 assemblyinfo 中引用为:[程序集:log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.xml", Watch = true)]。只要 xml 文件与 log4net.dll 位于同一目录中,一切就可以正常工作。

然而,现在我已将 log4net 添加到 GAC,它不再拾取 xml 文件。

有谁知道我需要更改什么才能让它再次拾取 XML 文件?在程序集引用中对补丁进行硬编码是唯一的方法吗?

非常感谢

I am using log4net during my development, as as part of a project constraint, I now need to add it to the Global Assembly Cache.

The logging definitions are in a file Log4Net.xml. That file is referenced in my assemblyinfo as: [assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.xml", Watch = true)]. So long as the xml file was in the same directory as the log4net.dll, everything has been working fine.

However now that I've added log4net to the GAC, it is no longer picking up the xml file.

Does anyone know what I need to change in order to have it pick up the XML file again? Is hardcoding the patch in the assembly reference the only way?

Many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

作死小能手 2024-09-05 18:25:50

log4net 期望配置文件位于以下返回的路径中:

 System.AppDomain.CurrentDomain.BaseDirectory

让您的应用程序将此信息打印到某个文件,然后您就知道需要将配置文件放置在哪里。

当然还有其他解决方案,但是这样你就不能再使用该属性了。直接调用ConfigureAndWatch()方法可以让你自己找出配置文件在哪里;您甚至可以决定一个位置(不必是硬编码路径)。

log4net expects the config file to be in the path returned by:

 System.AppDomain.CurrentDomain.BaseDirectory

Let your application print this information to some file and then you know where you need to place the config file.

Of course there are other solutions, but then you cannot use the attribute anymore. Calling the ConfigureAndWatch() method directly allows you to figure out yourself where the config file is; you can even decide on a location (does not have to be a hard-coded path).

眼泪都笑了 2024-09-05 18:25:50

您可以确保 log4net.xml 文件设置为“始终复制”(右键单击 log4net.xml -> 属性 -> 复制到输出目录 = 始终复制)。为了确保您的配置文件被复制,您应该检查 bin\debug 或 bin\release 目录,并验证 log4net.xml 文件是否存在于您的应用程序执行的同一目录中。

如果这不起作用,那么您可以尝试在 log4net 中启用内部调试。要启用内部调试,请将以下键添加到您的 app.config 文件中。这会将内部 log4net 调试消息发送到 Visual Studio 输出窗口(查看 -> 输出)。

<configuration>
  <appSettings>
    <add key="log4net.Internal.Debug" value="true"/>
  </appSettings>
</configuration>

有关 log4net 内部调试的更多信息,您可以查看 Phil Haack 的博客文章此处。

如果所有其他方法都失败,您可以打开内部调试并通过调用 log4net 的 XmlConfigurator.ConfigureAndWatch 方法显式加载配置。

var fi = new FileInfo(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\log4net.xml");
XmlConfigurator.ConfigureAndWatch(fi);

You might ensure that your log4net.xml file is set to "Copy Always" (Right Click on log4net.xml -> Properties -> Copy to Output Directory = Copy always). To ensure that your config file is being copied, you should check your bin\debug or bin\release directory and verify that the log4net.xml file exists in the same directory that your application executes.

If that doesn't workthen you can try enabling internal debugging in log4net. To enable internal debugging, add the following key to your app.config file. This will send internal log4net debug messages to your Visual Studio Output window (View -> Output).

<configuration>
  <appSettings>
    <add key="log4net.Internal.Debug" value="true"/>
  </appSettings>
</configuration>

For more information on log4net's internal debugging, you might check Phil Haack's blog post here.

If all else fails, you can turn on internal debugging and explicitly load the configuration by calling log4net's XmlConfigurator.ConfigureAndWatch method.

var fi = new FileInfo(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\log4net.xml");
XmlConfigurator.ConfigureAndWatch(fi);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文