Asp.net 使用单独的项目记录Log4Net时的Web.config配置

Asp.net 使用单独的项目记录Log4Net时的Web.config配置,asp.net,log4net,Asp.net,Log4net,我有一个包含2个项目的Visual Studio解决方案: Web应用程序 用于日志记录的库项目 我的Web应用程序项目没有Log4Net引用。日志项目是一个引用了log4netdll的项目,它有一个Manager类,我在其中拥有所有日志方法 现在,我想知道我必须在Web应用程序Web.Config文件中做什么,我在internet上看到我必须添加一个部分: <section name="log4net" type="log4net.Config.Log4NetConfig

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

  • Web应用程序
  • 用于日志记录的库项目
  • 我的Web应用程序项目没有Log4Net引用。日志项目是一个引用了log4netdll的项目,它有一个Manager类,我在其中拥有所有日志方法

    现在,我想知道我必须在Web应用程序Web.Config文件中做什么,我在internet上看到我必须添加一个部分:

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

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

    是的,您可以为Log4Net添加
    部分
    元素,而无需在web应用程序项目中引用
    Log4Net.dll


    可以肯定的是,我在VisualStudio2013中通过一个以.NET4.0为目标的scratch web应用程序项目(WebApplication1)和类库(ClassLibrary1)仔细检查了这一点。ClassLibrary1引用了
    log4net.dll
    ,但WebApplication1没有引用;它只是引用了ClassLibrary1,在它的
    Web.config
    中有一个log4net
    部分和相关的
    log4net
    元素:通过log4net登录到一个文本文件工作得很好。

    您似乎忘记了配置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。

    您尝试过吗?如果您使用的是源代码管理(当然,您应该这样做),如果您的实验出错,您可以简单地进行恢复。或者,一个简单的临时解决方案就足以回答您的问题。我从经验中知道,是的,你可以做你想做的事,而不用在你的web应用程序项目中添加对
    log4net.dll
    的引用;但我很快用一个scratch解决方案确认了这一点,并发布了最终的答案。不一定。当然,OP将需要解决这个问题(在类库中),就像我在进行双重检查时所做的那样。这真的是一个无关的问题,没有任何迹象表明OP不能理解那么多;但好吧,事实上有。从log4net开始是一个非常常见的错误。如果日志记录能够工作,OP将能够自己测试它,这是毫无疑问的。