Pac*_*chu 4 c# logging log4net
我正在使用它log4net来创建日志,但它没有执行任何操作。
这里是app.config:
<?xml version="1.0" encoding="utf-8">
<configuration>
<configSection>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSection>
<log4net>
<appender name="WriteToFile" type="log4net.Appender.FileAppender">
<file value="log.txt" />
<layout ="log4net.Layout.SimpleLayout" />
</appender>
<root>
<level value="ALL" />
<appender-ref ref="WriteToFile"/>
</root>
</log4net>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我有以下行AssemblyInfo.cs:
[assembly: log4net.Config.XmlConfigur(ConfigFile ="App.config", Watch= true)]
Run Code Online (Sandbox Code Playgroud)
这是写入文件的尝试:
private static readonly ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public void write()
{
log.Info("Some text here");
}
Run Code Online (Sandbox Code Playgroud)
以及以下行:
log4net.Config.XmlConfigurator.Configure();
Run Code Online (Sandbox Code Playgroud)
如果这是一个可执行文件,我想您的配置文件不会App.config在生成的 bin 文件夹中调用,而是在MyApp.exe.config. 所以你可以尝试解决这个问题:
ConfigFile ="App.config"
Run Code Online (Sandbox Code Playgroud)
如果您使用默认值,您还可以跳过配置文件名:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
Run Code Online (Sandbox Code Playgroud)
还要确保运行应用程序的帐户对您希望写入日志文件的文件夹具有写入权限。
更新:
分步指南:
在您的 App.config 中使用以下内容:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="WriteToFile" type="log4net.Appender.FileAppender">
<file value="log.txt" />
<layout type="log4net.Layout.SimpleLayout" />
</appender>
<root>
<level value="ALL" />
<appender-ref ref="WriteToFile" />
</root>
</log4net>
</configuration>
Run Code Online (Sandbox Code Playgroud)在你的Program.cs:
using log4net;
using System.Reflection;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace ConsoleApplication1
{
class Program
{
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
static void Main(string[] args)
{
log.Info("Some text here");
}
}
}
Run Code Online (Sandbox Code Playgroud)