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

时间:2016-12-18 15:44:45

标签: log4net

感谢您提供的任何帮助?不确定我做错了什么......

我正在DataFrame.from_records

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

https://github.com/JohnGoldInc/ErrorUnit.Logger_log4net的第37行有Count = 0 Appenders,即使我的日志显示已加载了appender:

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/ErrorUnitLogger.cs):

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 []
...

谢谢!

3 个答案:

答案 0 :(得分:1)

没有看顶级记录器,要获得顶级记录器参考.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;

答案 1 :(得分:1)

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

从您的日志中,您似乎正在将AdoNetAppender添加到&#39; root&#39;记录器,所以如果你想得到所有的追根者&#39; root&#39;您可能需要访问&#39; root&#39;记录器(位于层次结构的顶部)。在这种情况下,访问父记录器对您有用,因为&#39; root&#39;恰好是您拥有的ErrorUnitLogger的父级...但通常情况下这可能并非总是如此

如果您还不知道,可以访问&#39; root&#39;像这样的记录器

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

Logger rootLogger = hierarchy.Root;

答案 2 :(得分:0)

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

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