我真的希望有人可以帮助我。这真让我疯狂 我有一个简单的简单Windows窗体应用程序,我正在尝试使用log4net库进行日志记录(我只是在这个项目中测试它,因为它在我的主项目中没有用)。
所以我有常规的Form1.cs,app.config,AssemblyInfo.cs和Program.cs。
在我的app.config中,我有:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file value="log-file.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<header value="[Your Header text here]" />
<footer value="[Your Footer text here]" />
<conversionPattern value="%date [%thread] %-5level %logger [%ndc]
<%property{auth}> - %message%newline" />
</layout>
</appender>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
在Form1.cs中:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using log4net;
using log4net.Config;
namespace log4net.test
{
public partial class Form1 : Form
{
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
log4net.Config.XmlConfigurator.Configure();
log.Debug("This is a DEBUG level message. The most VERBOSE level.");
log.Info("Extended information, with higher importance than the Debug call");
log.Warn("An unexpected but recoverable situation occurred");
}
}
}
在AssemblyInfo.cs文件的末尾,我添加了:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "app.config", Watch = true)]
当我调试并转到button1_Click时,我发现没有任何事情发生。日志对象有:
IsInfoEnabled,IsDebugEnabled,IsErrorEnabled,IsWarnEnabled设置为false,没有任何反应。
我一直试图找到一整天都没有的解决方案。有人可以帮忙吗?
答案 0 :(得分:0)
您需要在配置中设置根日志记录级别:
</appender>
<!-- Set root logger level to DEBUG and its only appender to A1 -->
<root>
<level value="DEBUG" />
<appender-ref ref="A1" />
</root>
</log4net>
答案 1 :(得分:0)
问题出在这一行:
Error:Could not find com.google.android.gms:play-services-base:9.2.1.
Required by:
my-project:my-app:unspecified
<a href="searchInBuildFiles">Search in build.gradle files</a>
在运行时,bin目录中没有文件“app.config”。您必须指定正确的文件名,如下所示:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "app.config", Watch = true)]
或更好地省略名称
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "<your assembly name>.exe.config", Watch = true)]
我最近遇到了类似的问题。如果日志记录不起作用,我通常会启用调试输出,如下所示:How to track down log4net problems
答案 2 :(得分:0)
您还需要设置log4net的配置部分。设置配置部分允许log4net从app.config
参见下面的内容中读取设置:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
/// Add this section before the log4net node
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file value="log-file.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<header value="[Your Header text here]" />
<footer value="[Your Footer text here]" />
<conversionPattern value="%date [%thread] %-5level %logger [%ndc]
<%property{auth}> - %message%newline" />
</layout>
</appender>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>