Log4Net不从类库进行日志记录

Log4Net不从类库进行日志记录,log4net,class-library,Log4net,Class Library,我正在Windows7中使用C#开发.NETFramework4.0,并尝试从类库中登录,但不起作用。我正在运行我的应用程序,没有错误,但我的日志文件也没有任何变化,控制台也没有 这是我的代码: 这是我的App.config文件: <?xml version="1.0"?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4Ne

我正在Windows7中使用C#开发.NETFramework4.0,并尝试从类库中登录,但不起作用。我正在运行我的应用程序,没有错误,但我的日志文件也没有任何变化,控制台也没有

这是我的代码:

这是我的
App.config
文件:

<?xml version="1.0"?>
<configuration>

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


  <add key="log4net.config" value="config.log4net"/>

  <log4net>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{ABSOLUTE} [%thread] %level %logger - %message%newlineExtra Info: %property{testProperty}%newline%exception"/>
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="DEBUG"/>
        <levelMax value="FATAL"/>
      </filter>
    </appender>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="MyLogFile.txt"/>
      <appendToFile value="true"/>
      <rollingStyle value="Size"/>
      <maxSizeRollBackups value="5"/>
      <maximumFileSize value="10MB"/>
      <staticLogFileName value="true"/>
      <filter type="log4net.Filter.StringMatchFilter">
        <stringToMatch value="debug"/>
      </filter>
      <filter type="log4net.Filter.StringMatchFilter">
        <stringToMatch value="error"/>
      </filter>
      <filter type="log4net.Filter.DenyAllFilter"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/>
      </layout>
    </appender>
    <root>
      <level value="DEBUG"/>
      <appender-ref ref="ConsoleAppender"/>
      <appender-ref ref="RollingFileAppender"/>
    </root>
    <logger name="MyApplication">
      <level value="DEBUG"/>
      <appender-ref ref="ConsoleAppender"/>
      <appender-ref ref="RollingFileAppender"/>
    </logger>
  </log4net>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>

</configuration>
这是我试图记录的文件中的内容:

private static readonly ILog log = LogManager.GetLogger(
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);


log.Debug("Testing");
当我运行程序时,什么也没发生。有人知道原因吗?

类似于,app.config、log4net配置和AssemblyInfo.cs配置程序都需要放置在主机应用程序中

假设我将project
Console
作为要运行的控制台项目,并将
Library
作为库项目
Console
需要一个包含上述log4net配置的app.config文件,AssemblyInfo.cs需要包含XmlConfigurator代码
Library
不需要这两个函数中的任何一个,它将通过使用
LogManager
调用来工作

Console                 > This is the project that you will run.
    Properties
        AssemblyInfo.cs > Place [assembly: log4net.Config.XmlConfigurator(Watch = true)] here.
    app.config          > Place log4net XML configuration here.
    Program.cs          > Start of application.

Library
    Properties
        AssemblyInfo.cs > Leave this file alone.
    Foo.cs              > Create the private readonly property.
        SomeMethod()    > log.Debug("Test");
如果你看这张照片,上面写着:

“因此,如果使用配置属性,则必须调用log4net 允许它读取属性。对LogManager.GetLogger的简单调用将导致调用程序集上的属性 被读取和处理。因此,在应用程序启动期间,必须尽早进行日志记录调用,以及 当然是在加载和调用任何外部程序集之前。


当程序集属性在类库(即外部程序集)中定义时,会变得很棘手。你能改用
log4net.Config.XmlConfigurator.Configure(path)
吗?

如果有人还在看这个问题,并且要添加到前面的答案中,你需要在你的主机应用程序中实例化一个记录器,然后才能从类库中登录。

这对我来说很有用。您是将AssemblyInfo.cs的代码放在库项目中,还是放在主项目中?在库项目中。这一行是用来做什么的<代码>我尝试使用log4net.Config.XmlConfigurator.Configure()但随后我收到了这个错误堆栈:log4net:error未能在应用程序的.config文件中找到配置节“log4net”。根据上述解决方案,如果控制台是一个具有接口
ILogService
的项目,并且库是控制台应用程序中声明的服务的
log4net
实现,我们必须在控制台应用程序中添加对Log4Net的引用。这是最佳实践吗?这是我的问题。我正在尝试为安装脚本执行.dll类。它需要在我的主应用程序运行之前运行。我想这就是为什么它没有日志记录。
Console                 > This is the project that you will run.
    Properties
        AssemblyInfo.cs > Place [assembly: log4net.Config.XmlConfigurator(Watch = true)] here.
    app.config          > Place log4net XML configuration here.
    Program.cs          > Start of application.

Library
    Properties
        AssemblyInfo.cs > Leave this file alone.
    Foo.cs              > Create the private readonly property.
        SomeMethod()    > log.Debug("Test");