当我有以下 xml 配置来为 remotingappender 配置 log4net 时,一切正常。
<log4net>
<appender name="RemotingAppender" type="log4net.Appender.RemotingAppender" >
<sink value="tcp://localhost:8085/LoggingSink" />
<lossy value="false" />
<bufferSize value="1" />
<onlyFixPartialEventData value="true" />
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RemotingAppender" />
</root>
</log4net>
我想在代码中做同样的事情。我搜索了一下,找到了一个类似下面的例子。但我无法让它工作。
ILog log = log4net.LogManager.GetLogger("logName");
Repository.Hierarchy.Logger l = (Repository.Hierarchy.Logger)log.Logger;
// set level
l.Level = l.Hierarchy.LevelMap["ALL"];
// create appander
Appender.RemotingAppender remotingAppender = new Appender.RemotingAppender();
remotingAppender.Name = "custom";
remotingAppender.Sink = "tcp://localhost:8085/LoggingSink";
remotingAppender.Lossy = false;
remotingAppender.BufferSize = 1;
//remotingAppender.Fix = log4net.Core.FixFlags.All;
// create pattern
log4net.Layout.PatternLayout layout = new log4net.Layout.PatternLayout();
layout.ConversionPattern = "%d [%thread] %-5p %c [%a] - %m [%line] [%M]%n";
layout.ActivateOptions();
remotingAppender.Layout = layout;
remotingAppender.ActivateOptions();
// add appender
l.AddAppender(remotingAppender);
// perform logging (doesnt work)
log.Warn("my warning");
log.Error("my error");
我的代码中缺少什么?