我对log4net缺少什么 - 没有创建日志文件

时间:2010-04-12 05:30:39

标签: vb.net logging log4net

我试图在VB.NET应用程序中使用log4net,原因不明原因是它没有创建日志文件。

这是我的app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
       <configSections>
    <section name="log4net"
    type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <appender name="FileAppender" type="log4net.Appender.FileAppender">
      <file value="c:\log-file.txt" />
      <appendToFile value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="FileAppender" />
    </root>

  </log4net>
</configuration>

这是应用代码

Imports log4net
Public Class Form1
    Dim log As ILog
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        log.Error("test")
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        log4net.Config.XmlConfigurator.Configure()
        log = log4net.LogManager.GetLogger("TestThings")
    End Sub
End Class

“TestThings”是VS项目的名称。我错过了什么?

编辑〜我以管理员身份登录,我使用的是最新的log4net.dll

2 个答案:

答案 0 :(得分:3)

试试这个:

更改

< appender name="FileAppender" type="log4net.Appender.FileAppender" >

< appender name="FileAppender" type="log4net.Appender.FileAppender, log4net" >

将log4net也添加到

< layout type="log4net.Layout.PatternLayout" >

答案 1 :(得分:0)

在滚动文件追加器的配置中尝试此更改

<log4net>
    <appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="log\web.app.log" />
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="DEBUG" />
        <acceptOnMatch value="true" />
      </filter>
      <threshold value="DEBUG" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <datePattern value=".yyyyMMdd.lo\g" />
      <maximumFileSize value="5MB" />
      <maxSizeRollBackups value="-1" />
      <countDirection value="1" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="FileAppender" />
    </root>
    <logger name="webapplogger">
      <level value="debug" />
      <appender-ref ref="FileAppender" />
    </logger>
  </log4net>

在您的代码(表单加载或网页加载或IOC)初始化中,调用

log4net.Config.XmlConfigurator.Configure()
        log = log4net.LogManager.GetLogger("webapplogger")