这是我的课程:
internal class App : IDisposable
{
private static readonly ILog _log = LogManager.GetLogger(typeof(App));
internal void Start()
{
_log.Info("info");
_log.Debug("debug");
_log.Warn("warn");
_log.Error("error");
_log.Fatal("fatal");
Console.WriteLine("test");
}
// ...
}
主要方法:
public static void Main()
{
var dir = AppDomain.CurrentDomain.BaseDirectory;
XmlConfigurator.ConfigureAndWatch(new Hierarchy(), new FileInfo(Path.Combine(dir, "log4net.xml")));
// ...
}
.csproj条目:
<ItemGroup>
<None Include="../*.env" CopyToOutputDirectory="Always" />
<None Include="../log4net.xml" CopyToOutputDirectory="Always" />
</ItemGroup>
log4net.xml:
<log4net xmlns="http://csharptest.net/downloads/schema/log4net.xsd">
<appender name="console-default" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger - %message%newline"/>
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="console-default"/>
</root>
</log4net>
我得到的
我也尝试添加
using log4net.Config;
[assembly: XmlConfigurator(Watch = true)]
在Properties/AssemblyInfo.cs
同一件事是,如果我用FileAppender
替换appender,那只是在创建一个空文件
log4net.xml
文件将每个内部版本复制到可执行文件附近的内部目录
log4net
版本2.0.8
正确读取配置文件。如果我更改某些内容,则会在初始化时引发错误
答案 0 :(得分:1)
将ILoggerRepository
实例而不是Hierarchy
传递到XmlConfigurator.ConfigureAndWatch
。
var dir = AppDomain.CurrentDomain.BaseDirectory;
ILoggerRepository repository = log4net.LogManager.GetRepository(Assembly.GetCallingAssembly());
XmlConfigurator.ConfigureAndWatch(repository, new FileInfo(Path.Combine(dir, "log4net.config")));
没有理由在配置上使用xml namespace
声明;使用
<log4net>
代替
<log4net mlns="http://csharptest.net/downloads/schema/log4net.xsd">