log4net 的 EventLogAppender 无法记录日志。

12

使用以下配置不会发生任何事情。

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <log4net>
    <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
  </log4net>
</configuration>

Form1.cs(示例)

public partial class Form1 : Form
{
    private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

    public Form1()
    {
        InitializeComponent();

        log.Fatal("Test!");
    }
}

使用哪个版本的工作室? - Robbie Dee
Visual Studio 2012 + Windows 8。 - timmkrause
3个回答

15
你缺少根配置,需要添加类似以下内容的代码:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
      <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <startup>
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
  </startup>
  <log4net debug="true">
      <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
        <applicationName value="MyApp" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
        </layout>
      </appender>


      <root>
        <level value="All" />
        <appender-ref ref="EventLogAppender" />
      </root>


  </log4net>
</configuration>

注意,如果你的程序名为app.exe,则需要一个名为app.exe的事件日志源。如果该源不存在,则Log4net将尝试创建它,但这需要管理员权限,因此您可能需要至少以管理员身份运行程序一次才能创建此事件源。为避免这种情况,事件源通常会在安装过程中作为其一部分被创建,该过程已经在管理员权限下运行。

结合同事的代码,最终它终于起作用了。谢谢! :) 还有一个缺陷:只有在我启动 .exe 文件本身时才会显示日志条目,但当我按下 f5 时却没有。对于这个最后一个问题有什么想法吗? - timmkrause
4
为避免事件记录源像这样更改,您可以在配置文件中设置ApplicationName属性。 - sgmoore
那个提示使你的回答更加棒极了! :-) - timmkrause
请问在应用程序的.config文件中,"ApplicationName"属性应该放在哪个位置? - serge
<applicationName value="MyApp" /> 应该放在 EventLogAppender 部分,不一定是你的 app.config,但肯定是你的 log4net 配置文件中。 - Shaun Wilson
显示剩余2条评论

6

您需要添加:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

在你的项目的AssemblyInfo.cs文件中添加以下代码。这将初始化日志记录器并将log4net放入名为log4net.config的文件中。

我已经尝试过了,然后又将其删除,因为它没有任何效果。 - timmkrause
抱歉,你是对的。在我添加了sgmoore提供的<root>元素后,它仍然无法工作,但在我添加了你在AssemblyInfo.cs中的那行代码后,它就可以工作了。 - timmkrause

0
确保你的用户拥有写入权限: HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\EventLog\MyApp 如果你想添加更多的日志记录器,必须遵循这个命名规则:
<root name="EventLog">
  <level value="ALL"/>
  <appender-ref ref="FirstLog"/>
</root>

<logger name="FileLogger" additivity="false">
  <level value="ALL" />
  <appender-ref ref="Second_Log" />
</logger>

祝你好运。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接