Log4Net 不工作

发布于 2024-09-15 09:27:40 字数 3520 浏览 3 评论 0原文

我正在 Windows XP 中以管理员用户身份使用 C# 开发 .NET Framework 3.5,并使用 log4net 进行 .NET Framework 2.0 二进制引用。

我尝试过 log4net 缺少什么 - 无日志文件已创建,但它对我不起作用。

我将以下内容添加到 AssemblyInfo.cs 中:

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

并创建了一个文件“Log4Net.config”,如下所示,但是,当我运行控制台应用程序时,在项目中找不到任何日志文件。我想念什么?

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>
  <log4net>

    <!-- 
            log4net documentation http://logging.apache.org/log4net/
            Logging levels in order of increasing priority:
                * ALL
                * DEBUG
                * INFO
                * WARN
                * ERROR
                * FATAL
                * OFF
        -->

    <appender name="DebugLog" type="log4net.Appender.FileAppender">
      <file value="MyLog_debug.log" />
      <appendToFile value="true" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <maximumFileSize value="5MB" />
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="DEBUG" />
        <levelMax value="DEBUG" />
      </filter>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%newline%date %-5level %logger - %message%newline" />
      </layout>
    </appender>

    <appender name="InfoLog" type="log4net.Appender.RollingFileAppender">
      <file value="MyLog_info.log" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <datePattern value="yyyyMMdd" />
      <maxSizeRollBackups value="7" />
      <maximumFileSize value="5MB" />
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="INFO" />
        <levelMax value="WARN" />
      </filter>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%newline%date %-5level %logger - %message%newline" />
      </layout>
    </appender>

    <appender name="ErrorLog" type="log4net.Appender.RollingFileAppender">
      <file value="MyLog_error.log" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <datePattern value="yyyyMMdd" />
      <maxSizeRollBackups value="30" />
      <maximumFileSize value="5MB" />
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="ERROR" />
        <levelMax value="FATAL" />
      </filter>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%newline%date %-5level %logger - %message%newline" />
      </layout>
    </appender>

    <!-- Set the default logging level and add the active appenders -->
    <root>
      <level value="ALL" />
      <appender-ref ref="DebugLog" />
      <appender-ref ref="InfoLog" />
      <appender-ref ref="ErrorLog" />
    </root>

  </log4net>
</configuration>

I am working on .NET Framework 3.5 using C# in Windows XP as administrator user, using log4net for .NET Framework 2.0 binary reference.

I had tried What am I missing with log4net - No log file created but it does`t work for me.

I add the following into AssemblyInfo.cs:

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

And created a file "Log4Net.config" as below, however, when I run my console application, cannot find any log file in my project. What do I miss?

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>
  <log4net>

    <!-- 
            log4net documentation http://logging.apache.org/log4net/
            Logging levels in order of increasing priority:
                * ALL
                * DEBUG
                * INFO
                * WARN
                * ERROR
                * FATAL
                * OFF
        -->

    <appender name="DebugLog" type="log4net.Appender.FileAppender">
      <file value="MyLog_debug.log" />
      <appendToFile value="true" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <maximumFileSize value="5MB" />
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="DEBUG" />
        <levelMax value="DEBUG" />
      </filter>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%newline%date %-5level %logger - %message%newline" />
      </layout>
    </appender>

    <appender name="InfoLog" type="log4net.Appender.RollingFileAppender">
      <file value="MyLog_info.log" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <datePattern value="yyyyMMdd" />
      <maxSizeRollBackups value="7" />
      <maximumFileSize value="5MB" />
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="INFO" />
        <levelMax value="WARN" />
      </filter>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%newline%date %-5level %logger - %message%newline" />
      </layout>
    </appender>

    <appender name="ErrorLog" type="log4net.Appender.RollingFileAppender">
      <file value="MyLog_error.log" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <datePattern value="yyyyMMdd" />
      <maxSizeRollBackups value="30" />
      <maximumFileSize value="5MB" />
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="ERROR" />
        <levelMax value="FATAL" />
      </filter>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%newline%date %-5level %logger - %message%newline" />
      </layout>
    </appender>

    <!-- Set the default logging level and add the active appenders -->
    <root>
      <level value="ALL" />
      <appender-ref ref="DebugLog" />
      <appender-ref ref="InfoLog" />
      <appender-ref ref="ErrorLog" />
    </root>

  </log4net>
</configuration>

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

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

发布评论

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

评论(2

两仪 2024-09-22 09:27:40

确保配置文件被复制到输出目录。

如果您使用的是 Visual Studio,请转到“Log4Net.config”属性,并确保“复制到输出目录”设置为“始终复制”或“如果较新则复制”。否则,只需确保将配置文件复制到输出目录即可。

另外, log4net.Appender.FileAppender 没有“maximumFileSize”属性,因此您需要从配置文件中删除它。

Make sure the config file is being copied to the output directory.

If you're using Visual Studio, go to the 'Log4Net.config' properties, and make sure that the 'Copy to Output Directory' is set to 'Copy always' or 'Copy if newer'. Otherwise, just make sure you copy the config file to the output directory.

Also, log4net.Appender.FileAppender does not have a 'maximumFileSize' property, so you'll want to remove that from your configuration file.

﹎☆浅夏丿初晴 2024-09-22 09:27:40

在您的 myproject.csproj 中添加以下包含内容。

    <Content Include="log4net.config">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>

In your myproject.csproj add following inside which include content.

    <Content Include="log4net.config">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文