9

I am working on .NET Framework 4.0 using C# in Windows 7, and trying to log from a class library but it's not working. I'm running my application without errors, but also nothing happens to my log file, neither to my console.

So, here is my code:

This is my App.config file:

<?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>

This is what I put into my AssemblyInfo.cs:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

This what is at the file i'm trying to log:

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


log.Debug("Testing");

When i run my program nothing happens. Someone knows why?

4

3 回答 3

22

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

假设我有一个项目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");
于 2013-11-13T18:01:53.937 回答
4

如果有人仍在查看此内容并添加到以前的答案中,您需要先在主机应用程序中实例化一个记录器,然后才能从类库中登录。

于 2015-05-08T08:17:12.273 回答
3

如果您查看log4net 文档中的程序集属性,它会说:

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

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

于 2013-11-13T18:02:24.407 回答