示例#1
0
 public void ValidMessageParsing() {
     var ex = new Exception();
     var lm = new LoggyMessage(LogLevel.Error, "test", ex);
     Assert.AreEqual(LogLevel.Error,lm.Level);
     Assert.AreEqual("test",lm.Message);
     Assert.AreEqual(ex, lm.Exception);
 }
 protected override void InternalWrite(LoggyMessage message) {
     if(message.Level<Level)return;
     object error = null;
     if (null != message.Exception) {
         error = new {
             type = message.Exception.GetType().Name,
             message = message.Exception.Message,
             stack = message.Exception.StackTrace
         };
     }
     
     var id = GetId(message);
     object mes = message.Message;
     if (message.Message.StartsWith("{") && message.Message.EndsWith("}")) {
         mes = message.Message.jsonify();
     }
     var m = new {
         level = message.Level,
         timestamp = message.Timestamp.ToUniversalTime(),
         logger = message.LoggerName,
         loggerpath = "/"+message.LoggerName.Replace(".","/"),
         message = mes,
         error,
         user = message.UserName,
         host = Environment.MachineName,
         _id = id
     };
     var j = m.stringify();
     Client.ExecuteCommand("/" + Index + "/" + Type + "/" + id, j);
 }
示例#3
0
 public static void Write(this ILoggy loggy, LogLevel level, params object[] args)
 {
     if (loggy.IsFor(level))
     {
         var message = new LoggyMessage(level, args);
         loggy.Write(message);
     }
 }
示例#4
0
        public void ValidMessageParsing()
        {
            var ex = new Exception();
            var lm = new LoggyMessage(LogLevel.Error, "test", ex);

            Assert.AreEqual(LogLevel.Error, lm.Level);
            Assert.AreEqual("test", lm.Message);
            Assert.AreEqual(ex, lm.Exception);
        }
示例#5
0
 protected override void InternalWrite(LoggyMessage message) {
     var xml = GetEventXml(message);
     lock (sync) {
         _messageBuffer.Push(xml);
         if (_messageBuffer.Count >= AutoFlushSize) {
             
             Flush();
         }
     }
 }
示例#6
0
 public  void Write(LoggyMessage message) {
     if (!Active) return;
     try {
         if (message.Visited.Contains(this.Id)) {
             return;
         }
         InternalWrite(message);
         message.Visited.Add(this.Id);
     }
     catch (Exception ex) {
         (Manager ?? Loggy.Manager).Get("_failsafe").Error(new{appendertype=GetType().Name,error=ex.ToString()}.stringify());
     }
 }
示例#7
0
        public void Write(LoggyMessage message)
        {
            if (!Active)
            {
                return;
            }
            var key = message.Level + ":" + message.Message;

            if (!string.IsNullOrWhiteSpace(message.LoggerName))
            {
                key += ":" + message.LoggerName;
            }
            Messages.Add(key);
        }
示例#8
0
 protected string GetText(LoggyMessage message) {
     if (string.IsNullOrWhiteSpace(message.Message) && null != message.Exception) {
         message.Message =
             message.Exception.GetType().Name + ": " +
             message.Exception.Message + Environment.NewLine;
         if (null != message.Exception.StackTrace) {
             message.Message +=
            message.Exception.StackTrace.Substring(0, Math.Min(message.Exception.StackTrace.Length, 200)) + "...";
         }
         
     }
     var result = message.Message;
     
     if (!string.IsNullOrWhiteSpace(Format)) {
         result = Format.Interpolate(message);
     }
     return result;
 }
示例#9
0
 public void Write(LoggyMessage message) {
     if (!Active) return;
     if (IsFor(message.Level)) {
         if (string.IsNullOrWhiteSpace(message.LoggerName)) {
             message.LoggerName = this.Name;
         }
         foreach (var appender in Appenders) {
             if (appender.Level <= message.Level) {
                
                 appender.Write(message);
             }
         }
         if (HasSubloggers) {
             foreach (var subLogger in SubLoggers) {
                 if (subLogger.IsFor(message.Level)) {
                     subLogger.Write(message);
                 }
             }
         }
     }
 }
 private object GetId(LoggyMessage message) {
     return message.Timestamp.ToUniversalTime().ToString("yyyyMMddHHmmss_") +
            Guid.NewGuid().ToString().GetMd5().Substring(0, 6);
 }
示例#11
0
 public static void Write(LoggyMessage message) {
     Default.Write(message);
 }
示例#12
0
 protected abstract void InternalWrite(LoggyMessage message);
示例#13
0
 protected override void InternalWrite(LoggyMessage message) {
     _output?.WriteLine(GetText(message));
 }
示例#14
0
        public string GetEventXml(LoggyMessage _message) {
            var username = "******";
            if (null != PrincipalSource) {
                username = PrincipalSource.CurrentUser.Identity.Name;
            }
            else {
                if (Applications.Application.HasCurrent) {
                    if (Applications.Application.Current.Principal != null) {
                        username = Applications.Application.Current.Principal.CurrentUser.Identity.Name;
                    }
                }
            }
            // The format:
            //<log4j:event logger="{LOGGER}" level="{LEVEL}" thread="{THREAD}" timestamp="{TIMESTAMP}">
            //  <log4j:message><![CDATA[{ERROR}]]></log4j:message>
            //  <log4j:NDC><![CDATA[{MESSAGE}]]></log4j:NDC>
            //  <log4j:throwable><![CDATA[{EXCEPTION}]]></log4j:throwable>
            //  <log4j:locationInfo class="org.apache.log4j.chainsaw.Generator" method="run" file="Generator.java" line="94"/>
            //  <log4j:properties>
            //	<log4j:data name="log4jmachinename" value="{SOURCE}"/>
            //	<log4j:data name="log4japp" value="{APP}"/>
            //  </log4j:properties>
            //</log4j:event>
            var category = _message.Level.ToString();
            var level = category;
            var message = GetText(_message);
            var loggername = _message.LoggerName;


            var builder = new StringBuilder();

            var settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;

            var writer = XmlWriter.Create(builder, settings);
            WriteLog4jElement(writer, "event");
            writer.WriteAttributeString("logger", loggername);
            writer.WriteAttributeString("timestamp", DateTime.Now.ToString("o"));

            writer.WriteAttributeString("level", level);
            writer.WriteAttributeString("thread", Thread.CurrentThread.ManagedThreadId.ToString());
            writer.WriteAttributeString("domain", Environment.MachineName);
            writer.WriteAttributeString("username", username);

            writer.WriteElementString("message", Only30KBytes(RemoveInvalidXmlChars(message)));

            WriteLog4jElement(writer, "properties");
            WriteLog4jElement(writer, "data");
            writer.WriteAttributeString("name", "log4netmachinename");
            writer.WriteAttributeString("value", Environment.MachineName);
            writer.WriteEndElement();


            writer.WriteEndElement();
            if (_message.Exception != null) writer.WriteElementString("exception", GetExceptionString(_message.Exception));

            writer.WriteEndElement();

            writer.Flush();
            return builder.ToString();
        }