Ejemplo n.º 1
0
 /// <summary>
 /// Gets a decimal based on a static decimal as a string, or a pointer to a property of given event.
 /// </summary>
 /// <param name="logger">Logging instance for logging programs.</param>
 /// <param name="value">The value to parse</param>
 /// <param name="eventData">The event to fetch the value from should the "value" point to an event property.</param>
 /// <returns></returns>
 public static decimal? GetDecimal(ILogger logger, string value, LogEventData eventData)
 {
     decimal result;
     if (!decimal.TryParse(value, NumberStyles.Float, CultureInfo.GetCultureInfo("en-us"), out result))
     {
         if (eventData.Properties.ContainsKey(value))
         {
             var p = eventData.Properties[value];
             if (p is TimeSpan)
             {
                 result = ((TimeSpan)p).Milliseconds;
             }
             else
             {
                 try
                 {
                     result = Convert.ToDecimal(p);
                 }
                 catch (FormatException)
                 {
                     logger.Warning("Unable to send value to Ducksboard. Could not parse event property {EventPropertyKey} with value {Value} to decimal.", value, eventData.Properties[value]);
                     return null;
                 }
             }
         }
         else
         {
             logger.Warning("Unable to send value to Ducksboard. Could not parse {Value}. Expected an decimal or an event property key.");
             return null;
         }
     }
     return result;
 }
Ejemplo n.º 2
0
        public void Should_extract_values()
        {
            var offset          = 10.Hours();
            var timestamp       = DateTimeOffset.Now.ToOffset(offset);
            var stackTrace      = Guid.NewGuid().ToString();
            var messageTemplate = Guid.NewGuid().ToString();
            var renderedMessage = Guid.NewGuid().ToString();
            var propKey         = "prop1";
            var propValue       = 2;

            var eventBuilder = new HerculesEventBuilder();

            eventBuilder
            .SetTimestamp(timestamp)
            .AddValue(LogEventTagNames.MessageTemplate, messageTemplate)
            .AddValue(LogEventTagNames.Message, renderedMessage)
            .AddValue(LogEventTagNames.StackTrace, stackTrace)
            .AddContainer(LogEventTagNames.Properties, b => b.AddValue(propKey, propValue))
            .AddContainer(LogEventTagNames.Exception, delegate {});

            var @event = eventBuilder.BuildEvent();

            var logEventData = new LogEventData(@event);

            logEventData.Timestamp.Should().Be(timestamp);
            logEventData.MessageTemplate.Should().Be(messageTemplate);
            logEventData.Message.Should().Be(renderedMessage);
            logEventData.StackTrace.Should().Be(stackTrace);
            logEventData.Properties.Should().NotBeNull();
            logEventData.Properties[propKey].AsInt.Should().Be(propValue);
            logEventData.Exception.Should().NotBeNull();
        }
        public void SerializeDeserializeTest()
        {
            var logEventData = new LogEventData
            {
                Level      = LogLevel.Error,
                Message    = "Hello!",
                Properties = new Dictionary <string, string> {
                    ["key"] = "val"
                },
                Timestamp  = DateTimeOffset.UtcNow,
                Exceptions = new List <LogEventException>
                {
                    new LogEventException
                    {
                        Message = "msg",
                        Module  = "MyModule",
                        Type    = "ExType",
                        Stack   = new List <LogEventStackFrame>
                        {
                            new LogEventStackFrame
                            {
                                Module       = "Mod1",
                                ColumnNumber = 12,
                                Filename     = "my.cs",
                                Function     = "hello",
                                LineNumber   = 2,
                                Source       = "hello()"
                            }
                        }
                    }
                }
            };

            GenericSerializeDeserializeTest.RunTest <LogEventData, LogEventDataSerializer>(logEventData);
        }
Ejemplo n.º 4
0
        public static Event <LogEventData> FromRaw(string eventId, uint eventType, LogEvent raw)
        {
            var properties = new Dictionary <string, object>();

            foreach (var prop in raw.Properties)
            {
                if (prop.Key == "@seqid" || prop.Key == "@i")
                {
                    continue;
                }

                properties.Add(prop.Key, ToData(prop.Value));
            }

            var data = new LogEventData
            {
                Id              = eventId,
                Level           = (LogEventLevel)(int)raw.Level,
                Exception       = raw.Exception?.ToString(),
                LocalTimestamp  = raw.Timestamp,
                MessageTemplate = raw.MessageTemplate.Text,
                Properties      = properties,
                RenderedMessage = raw.RenderMessage()
            };

            return(new Event <LogEventData>(eventId, eventType, raw.Timestamp.UtcDateTime, data));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Gets a string based on a static string, or a pointer to a property of given event.
        /// </summary>
        /// <param name="logger">Logging instance for logging programs.</param>
        /// <param name="value">The value to parse</param>
        /// <param name="eventData">The event to fetch the value from should the "value" point to an event property.</param>
        /// <returns></returns>
        public static string GetString(ILogger logger, string value, LogEventData eventData)
        {
            if (ReferenceEquals(value, null)) return null;

            if (eventData.Properties != null && eventData.Properties.ContainsKey(value))
            {
                return eventData.Properties[value].ToString();
            }
            return value;
        }
Ejemplo n.º 6
0
        private void OnLoggedEvent(string formattedEvent, LogEventData entry, long id)
        {
            this.idOfLastMessage = id;
            this.Entries.AddLast(entry);
            while (this.Entries.Count > this.MaximumEntries)
            {
                this.Entries.RemoveFirst();
            }

            this.LogEventReceived?.Invoke(this, new LogEntryReceivedEventArgs(entry));
        }
Ejemplo n.º 7
0
        public void Should_not_fail_on_empty_HerculesEvent()
        {
            var @event = new HerculesEvent(DateTimeOffset.Now, HerculesTags.Empty);

            var logEventData = new LogEventData(@event);

            logEventData.Exception.Should().BeNull();
            logEventData.Properties.Should().BeNull();
            logEventData.MessageTemplate.Should().BeNull();
            logEventData.StackTrace.Should().BeNull();
            logEventData.Message.Should().BeNull();
        }
Ejemplo n.º 8
0
        public virtual LogEventData ToLogEventData()
        {
            var logEventData = new LogEventData
            {
                Level      = LogLevel.Error,
                Timestamp  = DateTimeOffset.UtcNow,
                Properties = new Dictionary <string, string>()
            };

            LoadStringPropertiesToDictionary(this, logEventData.Properties);
            return(logEventData);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Processes a json-object.
        /// </summary>
        /// <param name="logEvent">Log event data.</param>
        private void ProcessObject(LogEventData logEvent)
        {
            var doc = new Document();

            doc.Add(new Field("Timestamp", DateTools.DateToString(logEvent.Timestamp, DateTools.Resolution.MILLISECOND),
                              Field.Store.NO, Field.Index.ANALYZED));
            doc.Add(new Field("Level", logEvent.Level.ToString(), Field.Store.NO, Field.Index.ANALYZED));
            doc.Add(new Field("Message", logEvent.Message, Field.Store.NO, Field.Index.ANALYZED));

            ProcessException(logEvent.Exception, doc);
            ProcessProperties(logEvent.Properties, doc);

            doc.Add(new Field("Raw", JsonConvert.SerializeObject(logEvent), Field.Store.YES, Field.Index.NO));

            Context.ActorSelection(ActorPaths.Indexer.Path).Tell(doc);
        }
Ejemplo n.º 10
0
        public static string GetString(this LogEventData data, string propertyName)
        {
            if (data?.Properties == null)
            {
                return(null);
            }

            if (!data.Properties.ContainsKey(propertyName))
            {
                return(null);
            }

            var value = data.Properties[propertyName];

            return(value?.ToString());
        }
Ejemplo n.º 11
0
        protected void Application_Error(object sender, EventArgs e)
        {
            try
            {
                if (Sitecore.Context.Database == null)
                {
                    Log.Warn(this + " : Sitecore.Context.Database null", this);
                    return;
                }

                Exception exception = Server.GetLastError();
                Assert.IsNotNull(exception, "exception");
                Log.Error(
                    this + " : Exception processing " + Sitecore.Context.RawUrl,
                    exception,
                    this);
                LogEventData logEventData = new LogEventData(
                    ValidatorResult.CriticalError,
                    DateTime.Now,
                    exception)
                {
                    Message = exception.Message,
                    Url     = Sitecore.Context.RawUrl,
                    User    = Sitecore.Context.User != null ? Sitecore.Context.User.Name : string.Empty,
                    Site    = Sitecore.Context.Site != null ? Sitecore.Context.Site.Name : string.Empty
                };

                LogRemoteEvent.Queue(
                    this,
                    Sitecore.Context.Database, logEventData);
            }
            catch (Exception ex)
            {
                try
                {
                    Log.Error(
                        this + " ; exception queuing log entry remote event : " + ex.Message,
                        ex,
                        this);
                }
                catch (Exception)
                {
                }
            }
        }
Ejemplo n.º 12
0
        public void PropertiesAreRetrievedFromTheEvent(string propertyPath, string expectedMarkdown)
        {
            var data = new LogEventData
            {
                Properties = new Dictionary <string, object>
                {
                    ["First"]  = null,
                    ["Second"] = 20,
                    ["Third"]  = new Dictionary <string, object>
                    {
                        ["Fourth"] = "test 1",
                        ["Fifth"]  = new Dictionary <string, object>
                        {
                            ["Sixth"]   = "test 2",
                            ["Seventh"] = 123
                        }
                    },
                    ["Eighth"] = new Dictionary <string, object>
                    {
                        ["Excluded"] = true
                    }
                }
            };

            var evt    = new Event <LogEventData>("event-123", 4, DateTime.UtcNow, data);
            var config = new PropertyConfig
            {
                ExcludedProperties = new List <string>
                {
                    "Eighth"
                },
                JsonSerializedProperties = new List <string>
                {
                    "Third.Fifth"
                }
            };

            var actual = SeqEvents.GetProperty(evt, propertyPath, config);

            Assert.Equal(expectedMarkdown, actual);
        }
Ejemplo n.º 13
0
        public void Emit(SerilogEvent logEvent)
        {
            var airlockClient = getAirlockClient();
            var routingKey    = getRoutingKey();

            if (airlockClient == null || string.IsNullOrEmpty(routingKey))
            {
                return;
            }
            var logEventData = new LogEventData
            {
                Timestamp  = logEvent.Timestamp,
                Level      = TranslateLevel(logEvent.Level),
                Message    = logEvent.MessageTemplate.Render(logEvent.Properties).Truncate(maxMessageLength),
                Exceptions = logEvent.Exception.Parse(), // todo (andrew, 17.01.2018): maybe truncate if serialized Exceptions list has size > 32 kb
                Properties = logEvent.Properties.ToDictionary(x => x.Key, x => x.Value.ToString())
            };

            // todo (spaceorc, 13.10.2017) make "host" constant somewhere in Vostok.Core/LogPropertyNames.cs
            logEventData.Properties["host"] = HostnameProvider.Get();

            airlockClient.Push(routingKey, logEventData, logEventData.Timestamp);
        }
Ejemplo n.º 14
0
        public HandlerTests()
        {
            var log           = new ConsoleLog();
            var metricScope   = Substitute.For <IMetricScope>();
            var airlockClient = Substitute.For <IAirlockClient>();

            VostokHostingEnvironment.Current = new VostokHostingEnvironment()
            {
                Environment   = "dev",
                Log           = log,
                AirlockClient = airlockClient,
                MetricScope   = metricScope
            };
            airlockClient.When(c => c.Push(Arg.Any <string>(), Arg.Any <LogEventData>(), Arg.Any <DateTimeOffset?>())).Do(
                x =>
            {
                routingKey   = x.Arg <string>();
                logEventData = x.Arg <LogEventData>();
                log.Debug(logEventData.ToPrettyJson());
            });
            httpHandler = new HttpHandler(new FrontierSetings {
                SourceMapBlacklist = new [] { "diadoc.kontur.ru" }
            }, metricScope, log, airlockClient);
        }
Ejemplo n.º 15
0
 private void SendLog(LogEventData log)
 {
     Context.ActorOf <DocumentMapper>().Tell(log);
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Initializes a new instance of the <see cref="LogEntry"/> class.
 /// </summary>
 /// <param name="id">The unique identifier which is a sequence number.</param>
 /// <param name="formattedEvent">The formtted event as configured in the settings.</param>
 /// <param name="loggingEvent">The logging event data.</param>
 public LogEntry(long id, string formattedEvent, LogEventData loggingEvent)
 {
     this.Id             = id;
     this.FormattedEvent = formattedEvent;
     this.LoggingEvent   = loggingEvent;
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Initializes a new instance of the <see cref="LogEntryReceivedEventArgs"/> class.
 /// </summary>
 /// <param name="data">The log event data.</param>
 public LogEntryReceivedEventArgs(LogEventData data)
 {
     this.Data = data;
 }
Ejemplo n.º 18
0
 private void OnReceiveLog(LogEventData logEventData)
 {
 }
Ejemplo n.º 19
0
        protected override void Append(LoggingEvent loggingEvent)
        {
            // assume we don't care about the requested URL
            // for anything lower than warning
            if (loggingEvent.Level != Level.WARN &&
                loggingEvent.Level != Level.ERROR &&
                loggingEvent.Level != Level.FATAL)
            {
                base.Append(loggingEvent);
                return;
            }

            string url = null;

            if (HttpContext.Current != null)
            {
                // includes the protocol and hostname, so try that first
                url = HttpContext.Current.Request.Url.AbsoluteUri;
            }
            else if (!string.IsNullOrEmpty(Sitecore.Context.RawUrl))
            {
                // this does not, but is probably always empty
                // if HttpContext.Current is null
                url = Sitecore.Context.RawUrl;
            }

            LoggingEventData data =
                loggingEvent.GetLoggingEventData();

            if (!string.IsNullOrWhiteSpace(url))
            {
                data.Message += " URL: " + url;
            }

            if (Sitecore.Context.User != null)
            {
                // context user might be relevant
                // for diagnosing permission issues
                data.Message += " (" + Sitecore.Context.User.Name + ')';
            }

            LoggingEvent ev = new LoggingEvent(data);

            base.Append(ev);

            // avoid infinite recursion!
            if (data.Message.Contains("error raising remote event"))
            {
                base.Append(loggingEvent);
                return;
            }

            try
            {
                Database db = Sitecore.Context.Database;

                if (db == null)
                {
                    SiteInfo siteInfo = Sitecore.Configuration.Factory.GetSiteInfo(
                        Sitecore.Configuration.Settings.Preview.DefaultSite);
                    Assert.IsNotNull(
                        siteInfo,
                        "siteInfo : " + Sitecore.Configuration.Settings.Preview.DefaultSite);
                    db = Sitecore.Configuration.Factory.GetDatabase(
                        siteInfo.Database);
                    Assert.IsNotNull(db, "db : " + siteInfo.Database);
                }

                ValidatorResult result = ValidatorResult.CriticalError;

                if (loggingEvent.Level == Level.WARN)
                {
                    result = ValidatorResult.Warning;
                }
                else if (loggingEvent.Level == Level.FATAL)
                {
                    result = ValidatorResult.FatalError;
                }

                LogEventData logEventData = new LogEventData(
                    result,
                    DateTime.Now)
                {
                    Message = data.Message,
                    Url     = url,
                    User    = Sitecore.Context.User != null ? Sitecore.Context.User.Name : string.Empty,
                    Site    = Sitecore.Context.Site != null ? Sitecore.Context.Site.Name : string.Empty
                };

                if (!string.IsNullOrWhiteSpace(data.ExceptionString))
                {
                    logEventData.Message = data.ExceptionString;
                }

                LogRemoteEvent.Queue(
                    this,
                    db,
                    logEventData);
            }
            catch (Exception ex)
            {
                LoggingEventData log4netEventData =
                    new LoggingEventData();
                log4netEventData.Message =
                    this + " : error raising remote event : " + ex.Message;
                log4netEventData.LoggerName      = this.ToString();
                log4netEventData.Level           = Level.CRITICAL;
                log4netEventData.ExceptionString =
                    ex.GetType() + " : " + ex.Message + " : " + ex.StackTrace;
                LoggingEvent log4netEvent = new LoggingEvent(
                    log4netEventData);

                if (!string.IsNullOrWhiteSpace(url))
                {
                    data.Message += " URL: " + url;
                }

                if (Sitecore.Context.User != null)
                {
                    // context user might be relevant for diagnosing permission issues
                    data.Message += " (" + Sitecore.Context.User.Name + ')';
                }

                base.Append(log4netEvent);
                throw ex;
            }
        }
Ejemplo n.º 20
0
 static bool IsError(LogEventData log)
 => log.Type == LogType.Exception || log.Type == LogType.Error || log.Type == LogType.Assert;