log4net 缺少什么 - 未创建日志文件

发布于 2024-08-29 01:15:23 字数 1485 浏览 4 评论 0原文

我试图在 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

I am trying to use log4net in a VB.NET app for some unknown reason it's not creating the log file.

Here is my 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>

Here is the app code

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" is the name of the VS project. What am I missing?

Edit ~ I am logged in as admin and I am using the most recent log4net.dll

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十年九夏 2024-09-05 01:15:23

试试这个:

更改

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

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

将 log4net 也添加到

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

try this:

change

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

to

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

add log4net also to

< layout type="log4net.Layout.PatternLayout" >
沫雨熙 2024-09-05 01:15:23

尝试在滚动文件附加程序的配置中进行此更改

<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")

Try this change in the config for a rolling file appender

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

And in your code (form load or webpage load or IOC) intialization, call

log4net.Config.XmlConfigurator.Configure()
        log = log4net.LogManager.GetLogger("webapplogger")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文