Ejemplo n.º 1
0
 public void Enrich(global::Serilog.Events.LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
 {
     foreach (var kvp in Context.Properties.Current)
     {
         logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty(kvp.Key, kvp.Value));
     }
 }
Ejemplo n.º 2
0
        public void Emit(global::Serilog.Events.LogEvent logEvent)
        {
            if (_storage == null)
            {
                return;
            }
            if (logEvent == null)
            {
                return;
            }
            var key = Guid.NewGuid().ToString().Replace("-", string.Empty).ToUpper();

            //UpdateProperties(logEvent, key);
            //FilterProperties(logEvent);

            _storage.Upsert(key, new LogEvent
            {
                Key           = key,
                CorrelationId = GetCorrelationId(logEvent),
                Level         = logEvent.Level.ToString(),
                Message       = logEvent.RenderMessage(_formatProvider),
                Timestamp     = logEvent.Timestamp.DateTime,
                Properties    = GetProperties(logEvent)
            }, GetTags(logEvent), forceInsert: true, timestamp: logEvent.Timestamp.DateTime);
        }
Ejemplo n.º 3
0
        //private void UpdateProperties(global::Serilog.Events.LogEvent logEvent, string key)
        //{
        //    logEvent.AddOrUpdateProperty(new LogEventProperty("Message",
        //        new ScalarValue(logEvent.RenderMessage(_formatProvider))));
        //    logEvent.AddOrUpdateProperty(new LogEventProperty("DocSetKey",
        //        new ScalarValue(key)));
        //}

        private IEnumerable <string> GetTags(global::Serilog.Events.LogEvent logEvent)
        {
            if (logEvent.Properties.IsNullOrEmpty() || _propertiesAsTags.IsNullOrEmpty())
            {
                return(null);
            }
            return
                (logEvent.Properties.Where(p => _propertiesAsTags.Contains(p.Key))
                 .Select(p => p.Value != null ? p.Value.ToString().Trim('"') : string.Empty));
        }
Ejemplo n.º 4
0
        /// <inheritdoc />
        public void Log(LogEvent @event)
        {
            var serilogEvent = new global::Serilog.Events.LogEvent(
                @event.Timestamp,
                @event.Level.ToSerilogLogLevel(),
                @event.Exception,
                _messageTemplateCache.Get(@event.MessageTemplate),
                @event.GetSerilogProperties(_logger));

            _logger.Write(serilogEvent);
        }
Ejemplo n.º 5
0
        private static string GetCorrelationId(global::Serilog.Events.LogEvent logEvent)
        {
            LogEventPropertyValue correlationProp;

            logEvent.Properties.TryGetValue("CorrelationId", out correlationProp);
            string correlationId = null;

            if (correlationProp != null)
            {
                correlationId = correlationProp.ToString().Trim('"');
            }
            return(correlationId);
        }
Ejemplo n.º 6
0
        //private void FilterProperties(global::Serilog.Events.LogEvent logEvent)
        //{
        //    if (_propertiesWhiteList == null || !_propertiesWhiteList.Any()) return;
        //    foreach (var prop in logEvent.Properties.Where(prop => !_propertiesWhiteList.Contains(prop.Key)))
        //    {
        //        logEvent.RemovePropertyIfPresent(prop.Key);
        //    }
        //}

        private Dictionary <string, string> GetProperties(global::Serilog.Events.LogEvent logEvent)
        {
            if (logEvent.Properties.IsNullOrEmpty())
            {
                return(null);
            }
            if (_propertiesWhiteList == null || !_propertiesWhiteList.Any())
            {
                return(logEvent.Properties.ToDictionary(p => p.Key, p => p.Value != null ? p.Value.ToString().Trim('"') : string.Empty));
            }

            return(logEvent.Properties.Where(prop => _propertiesWhiteList.Contains(prop.Key))
                   .ToDictionary(prop => prop.Key, p => p.Value != null ? p.Value.ToString().Trim('"') : string.Empty));
        }
Ejemplo n.º 7
0
        public void Emit(global::Serilog.Events.LogEvent logEvent)
        {
            var vostokLogLevel = TranslateLevel(logEvent.Level);

            if (!log.IsEnabledFor(vostokLogLevel))
            {
                return;
            }
            var vostokLogEvent = new LogEvent(
                vostokLogLevel,
                logEvent.Exception,
                logEvent.MessageTemplate.Render(logEvent.Properties),
                new object[0]);

            foreach (var kvp in logEvent.Properties)
            {
                vostokLogEvent.AddPropertyIfAbsent(kvp.Key, kvp.Value);
            }
            log.Log(vostokLogEvent);
        }
Ejemplo n.º 8
0
 public void Emit(global::Serilog.Events.LogEvent logEvent)
 {
     Writes.Enqueue(logEvent);
 }
 public void Enrich(global::Serilog.Events.LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
 {
     logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("Thread", Thread.CurrentThread.Name ?? Thread.CurrentThread.ManagedThreadId.ToString()));
 }