c# - log4net 无法创建日志文件
问题描述
我第一次尝试使用 log4net 为组 c# 项目设置日志记录,虽然在运行我的代码时似乎没有出现任何问题,并且负责记录的行肯定正在执行,但没有创建日志文件,也没有记录任何内容。既不在日志文件中,也不在控制台中。
我的 log4net.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="console" />
<appender-ref ref="file" />
</root>
<appender name="console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger - %message%newline" />
</layout>
</appender>
<appender name="file" type="log4net.Appender.RollingFileAppender">
<file value="myapp.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
</appender>
</log4net>
</configuration>
我的 app.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.6.0" newVersion="4.0.6.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /></startup></configuration>
我还将该行[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config",Watch =true)]
添加到我的 AssemblyInfo.cs
代码本身大致如下:
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
log.Debug("this is a test log.");
还值得注意的是,我从一个辅助项目执行这个项目。
我尝试按照设置指南进行操作,但我一定错过了一些东西,我会非常感谢一些帮助。
解决方案
推荐阅读
- c# - 如何在 Xamarin 中创建图形?
- node.js - 发布请求后反应状态不更新?
- c# - 从 Activator.CreateInstance 创建对象后从接口访问方法
- sql - 如何在 SQL 中获取对象(具有所有键名)中的对象?
- flutter - 无法在同一个 HookWidget 中同时使用 useFuture() 和 useValueChanged()
- ruby - Array.each 的 Ruby 奇怪行为
- linux - 屏幕处理程序(例如.vi、less)如何工作?
- asp.net-core - 使用 Asp.net 核心和 Identityserver 4 从浏览器获取 401,但从邮递员获取 200
- p5.js - 如何在 Vue 3 中使用 p5.loadImage
- visual-studio-code - 在 Visual Studio 代码中使用 Tab 按钮自动完成后的智能感知问题 - 版本:1.52.1