【问题标题】:Web.config configuration when using separate Project for logging Log4Net使用单独的项目记录 Log4Net 时的 Web.config 配置
【发布时间】:2014-06-28 14:04:08
【问题描述】:

我有一个包含 2 个项目的 Visual Studio 解决方案:

  1. Web 应用程序
  2. 用于日志记录的库项目

我的 Web 应用程序项目没有 Log4Net 参考。 Logging Project 是一个引用 Log4Net dll 的项目,它有一个 Manager 类,我拥有所有的日志记录方法。

现在,我想知道在Web Application WEb.Config 文件中我要做什么,我在网上看到我必须添加一个Section:

 <section name="log4net" 
      type="log4net.Config.Log4NetConfigurationSectionHandler, 
            log4net"/>

但我担心的是我的 Web 项目没有 Log4Net dll 引用。有没有办法在不将 Log4Net dll 添加到我的 Web 项目的情况下做到这一点?

【问题讨论】:

  • 你试过了吗?如果您正在使用源代码控制(当然应该如此),如果您的实验出错,您可以简单地恢复。
  • 另外,一个简单的临时解决方案足以回答您的问题。我从经验中知道,是的,您可以做您想做的事,而无需在您的 Web 应用程序项目中添加对 log4net.dll 的引用;但我很快通过临时解决方案确认了这一点,并发布了结果答案。

标签: asp.net log4net


【解决方案1】:

是的,您可以为 log4net 添加 section 元素,而无需在您的 Web 应用程序项目中引用 log4net.dll

可以肯定的是,我在 Visual Studio 2013 中使用针对 .NET 4.0 的临时 Web 应用程序项目 (WebApplication1) 和类库 (ClassLibrary1) 仔细检查了这一点。ClassLibrary1 引用了 log4net.dll,但 WebApplication1 没有;它只是引用了 ClassLibrary1 并在其 Web.config 中有一个 log4net section 元素和相关的 log4net 元素:通过 log4net 登录到文本文件效果很好。

【讨论】:

    【解决方案2】:

    您似乎忘记配置 log4net。最佳做法是在您的 assably.cs 中添加一个配置属性:

    // Configure log4net using the .config file
    [assembly: log4net.Config.XmlConfigurator(Watch=true)]
    // This will cause log4net to look for a configuration file
    // called TestApp.exe.config in the application base
    // directory (i.e. the directory containing TestApp.exe)
    // The config file will be watched for changes.
    
    // Configure log4net using the .log4net file
    [assembly: log4net.Config.XmlConfigurator(ConfigFileExtension="log4net",Watch=true)]
    // This will cause log4net to look for a configuration file
    // called TestApp.exe.log4net in the application base
    // directory (i.e. the directory containing TestApp.ex
    

    您可以在您的 log4net dll 中添加该属性,您的 web 项目中无需引用 log4net。

    【讨论】:

    • 不一定。当然,OP 需要解决这个问题(在类库中),就像我在我放在一起的仔细检查中所做的那样。这确实是一个不相关的观点,没有任何迹象表明 OP 无法理解那么多;但没关系。
    • 其实有。从 log4net 开始,这是一个非常常见的错误。如果日志记录有效,那么 OP 将能够自己测试它,这将是毫无疑问的。
    猜你喜欢
    • 2018-09-13
    • 1970-01-01
    • 2010-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 2020-01-02
    相关资源
    最近更新 更多