没有 log4net Logger.Appenders 的问题,即使日志显示配置已成功加载

Issue with no log4net Logger.Appenders even though log shows config successfully loaded

如果您能提供任何帮助,我将不胜感激?不确定我做错了什么...

我正在 https://github.com/JohnGoldInc/ErrorUnit.Logger_log4net

为我的新 Visual Studio 工具 ErrorUnit 创建一个 log4net 兼容性库

https://github.com/JohnGoldInc/ErrorUnit.Logger_log4net/blob/master/ErrorUnitLogger.cs 的第 37 行有 Count=0 个附加程序,即使我的日志显示附加程序已加载:

16 namespace ErrorUnit.Logger_log4net
17 {
18    public class ErrorUnitLogger : ILogger
19    {
20        private static ILog log = LogManager.GetLogger(typeof(ErrorUnitLogger));
21
22        public IEnumerable<string> GetErrorUnitJson(DateTime afterdate)
23        {
24            var ErrorUnitJson = new ConcurrentBag<string>();
25            log4net.Util.LogLog.InternalDebugging = true; //todo remove
26            System.Diagnostics.Trace.Listeners.Add(new System.Diagnostics.TextWriterTraceListener(@"f:\Temp\Logger_log4net.log", "myListener"));
27            System.Diagnostics.Trace.AutoFlush = true;
28            System.Diagnostics.Trace.TraceInformation("Test Logger_log4net message.");
29
30            var config = log4net.Config.XmlConfigurator.Configure();
31            log = LogManager.GetLogger(typeof(ErrorUnitLogger));
32
33            // Parallel.ForEach(logs, log => {
34            var log4net_Logger = log.Logger as log4net.Repository.Hierarchy.Logger;
35            if (log4net_Logger != null)
36            {
37                Parallel.ForEach(log4net_Logger.Appenders.Cast<log4net.Appender.IAppender>(), appender =>

对于日志(没有省略号的日志在https://github.com/JohnGoldInc/ErrorUnit.Logger_log4net/blob/master/README.md):

devenv.exe Information: 0 : Test Logger_log4net message.
log4net: configuring repository [log4net-default-repository] using .config file section
log4net: Application config file is [F:\Documents\Visual Studio 2015\Projects\WebApplication1\WebApplication1.Tests\obj\Release]
log4net: Configuring Repository [log4net-default-repository]
log4net: Configuration update mode [Merge].
...
log4net: Created Appender [AdoNetAppender]
log4net: Adding appender named [AdoNetAppender] to logger [root].
log4net: Hierarchy Threshold []
...

谢谢!

您在配置 log4net 之前发送测试消息。首先配置 log4net 然后发送测试跟踪消息:

System.Diagnostics.Trace.TraceInformation("Test Logger_log4net message.");

没有查看顶级记录器,要获取顶级记录器请参考 .Parent 属性:

var log4net_Logger = log.Logger as log4net.Repository.Hierarchy.Logger;

while (log4net_Logger != null && log4net_Logger.Appenders.Count == 0)
    log4net_Logger = log4net_Logger.Parent;

我没有足够的声誉来为您的答案添加评论...所以在这里添加一个新答案,但这更像是评论

从您的日志来看,您似乎正在将 AdoNetAppender 添加到 'root' 记录器,因此如果您希望获得 'root' 的所有附加程序,您可能需要访问 'root'记录器(位于层次结构的顶部)。在这种情况下,访问父 Logger 对你有用,因为 'root' 恰好是你拥有的 ErrorUnitLogger 的父级......但通常情况下可能并不总是如此

如果您还不知道,您可以像这样访问 'root' 记录器

Hierarchy hierarchy = (Hierarchy)log4net.LogManager.GetRepository();

Logger rootLogger = hierarchy.Root;