log4net在开始时没有记录,在一段时间后启动

时间:2016-03-05 13:45:06

标签: log4net

我们一直在.net控制台应用中使用log4net。它用于从第一个条目将所有内容记录到控制台和SQL。突然间,记录器直到很久以后才写入我们的SQL,这很奇怪,考虑到:

Logger.Info("my message won't log")
Logger.Info("my message will log")

它不会记录第一个,而是记录第二个。似乎每次都在同一行代码中发生,仍在验证。我调试,停在第一个记录器,使用立即窗口并记录消息,这是有效的。似乎无法理解为什么。

但是所有的一切都被记录到控制台了!

对可能发生的事情或原因的看法?

自定义附加程序代码

namespace Dsmt.Logging
{
    public class HistoryPatternLayout : PatternLayout
    {
        public HistoryPatternLayout()
        {
            AddConverter(new ConverterInfo
            {
                Name = "migrationHistory",
                Type = typeof(MigrationHistoryConverter)
            });
        }
    }

    public class MigrationHistoryConverter : PatternConverter
    {
        protected override void Convert(TextWriter writer, object state)
        {
            if (state == null)
            {
                writer.Write(SystemInfo.NullText);
                return;
            }
            var loggingEvent = state as LoggingEvent;
            var historyInfo = loggingEvent!= null ? loggingEvent.MessageObject as HistoryLoggerInfo : null;
            if (historyInfo == null)
            {
                writer.Write(SystemInfo.NullText);
                return;
            }
            switch (this.Option.ToLower())
            {
                case "migrationhistoryid" :
                    writer.Write(historyInfo.MigrationHistoryId);
                    break;
                case "itemid" :
                    writer.Write(historyInfo.ItemID);
                    break;
                case "success" :
                    writer.Write(historyInfo.Success);
                    break;
                case "message" :
                    writer.Write(historyInfo.Message);
                    break;
                case "itemdatetime" :
                    writer.Write(historyInfo.ItemDateTime.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                    break;
                case "newlocation":
                    writer.Write(historyInfo.NewLocation);
                    break;

                default :
                    writer.Write(SystemInfo.NullText);
                    break;
            }
        }
    }

    public class HistoryLoggerInfo
    {
        public string MigrationHistoryId { get; set; }
        public string ItemID { get; set; }
        public bool Success { get; set; }
        public string Message { get; set; }
        public DateTime ItemDateTime { get; set; }

        public string NewLocation { get;set; }

        public override string ToString()
        {
            var serializeObject = JsonConvert.SerializeObject(this);
            //var serializeObject = JsonConvert.SerializeObject(this, new Log4NetDateTimeConverter());
            return serializeObject;
        }

        public class Log4NetDateTimeConverter : DateTimeConverterBase
        {
            public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
            {
                writer.WriteValue(((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss.fff"));
            }

            public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
            {
                return DateTime.Now.AddYears(-13);
            }
        }
    }

}

0 个答案:

没有答案