internal virtual void WriteMessage(LogMessageEntry entry)
        {
            var buff = System.Text.Encoding.ASCII.GetBytes(
                string.Format($"{entry.Message} (sent {DateTime.Now})")
                );

            int sent = _udpSender.Send(buff, buff.Length, _options.IPEndPoint);
        }
        public virtual void EnqueueMessage(LogMessageEntry message)
        {
            if (!_messageQueue.IsAddingCompleted)
            {
                try
                {
                    _messageQueue.Add(message);
                    return;
                }
                catch (InvalidOperationException) { }
            }

            // Adding is completed so just log the message
            try
            {
                WriteMessage(message);
            }
            catch (Exception) { }
        }
Пример #3
0
        public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            if (!IsEnabled(logLevel))
            {
                return;
            }

            try
            {
                // TODO: require LogMessageEntry in params?
                var entry = new LogMessageEntry {
                    LogLevel = logLevel,
                    EventId  = eventId,
                    Message  = formatter.Invoke(state, exception)
                };
                // Send log message
                _processor.EnqueueMessage(entry);
            }
            catch (Exception)
            {
                // SelfLog.WriteLine($"Failed to write event through {typeof(SerilogLogger).Name}: {ex}");
                throw;
            }
        }