/// <summary>
 /// Writes trace information, a data object and event information to the
 /// listener specific output.
 /// </summary>
 /// <param name="eventCache">
 /// A <see cref="T:System.Diagnostics.TraceEventCache"/> object that
 /// contains the current process ID, thread ID, and stack trace
 /// information.
 /// </param>
 /// <param name="source">
 /// A name used to identify the output, typically the name of the
 /// application that generated the trace event.
 /// </param>
 /// <param name="eventType">
 /// One of the <see cref="T:System.Diagnostics.TraceEventType"/> values
 /// specifying the type of event that has caused the trace.
 /// </param>
 /// <param name="id">A numeric identifier for the event.</param>
 /// <param name="data">The trace data to emit.</param>
 public sealed override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, params object[] data)
 {
     if (data != null)
     {
         TraceEventData eventData = AddCustomerIdAndSessionIdToEventDataIfTheyAreSet(new TraceEventData(eventCache, source, eventType, id));
         Trace(eventData, data);
     }
 }
        /// <summary>
        /// Writes trace information and a message to the listener specific
        /// output.
        /// </summary>
        /// <param name="e">
        /// A <see cref="TraceEventData"/> object that contains information about
        /// the trace event.
        /// </param>
        /// <param name="message">The message to write.</param>
        protected void Trace(TraceEventData e, string message)
        {
            var type = e.Type.GetValueOrDefault(TraceEventType.Information);

            if (ShouldTrace(e.Cache, e.Source, type, e.ID, message, null, null, null))
            {
                TraceInternal(e, message);
            }
        }
        private string ConstructLogMessageToWrite(TraceEventData e, string message)
        {
            string newMessage = "[" + e.Cache.DateTime + "] ";

            newMessage += "[" + GetTraceEventDataTypeWithInformationAsDefault(e) + "] ";
            newMessage += message;
            newMessage += " [" + e.Source + "]";
            return(newMessage);
        }
        /// <summary>
        /// Writes trace information and a formatted message to the listener
        /// specific output.
        /// </summary>
        /// <param name="e">
        /// A <see cref="TraceEventData"/> object that contains information about
        /// the trace event.
        /// </param>
        /// <param name="format">
        /// A format string that contains zero or more format items, which
        /// correspond to objects in the <paramref name="args"/> array.
        /// </param>
        /// <param name="args">
        /// An object array containing zero or more objects to format.
        /// </param>
        protected void Trace(TraceEventData e, string format, params object[] args)
        {
            var type = e.Type.GetValueOrDefault(TraceEventType.Information);

            if (ShouldTrace(e.Cache, e.Source, type, e.ID, format, args, null, null))
            {
                TraceInternal(e, string.Format(format, args));
            }
        }
        /// <summary>
        /// Writes trace information and a data object to the listener specific
        /// output if.
        /// </summary>
        /// <param name="e">
        /// A <see cref="TraceEventData"/> object that contains information about
        /// the trace event.
        /// </param>
        /// <param name="data">The trace data to write.</param>
        protected void Trace(TraceEventData e, object data)
        {
            var type = e.Type.GetValueOrDefault(TraceEventType.Information);

            if (ShouldTrace(e.Cache, e.Source, type, e.ID, null, null, data, null))
            {
                TraceInternal(e, data);
            }
        }
 private string GetTraceEventDataTypeWithInformationAsDefault(TraceEventData e)
 {
     if (string.IsNullOrEmpty(e.Type.ToString()))
     {
         return("Information");
     }
     else
     {
         return(e.Type.ToString());
     }
 }
 private TraceEventData AddCustomerIdAndSessionIdToEventDataIfTheyAreSet(TraceEventData eventData)
 {
     if (ADefaultCustomerIdIsSet(out Guid customerIdToUse))
     {
         eventData.CustomerID = customerIdToUse;
     }
     if (ADefaultSessionIdIsSet(out string sessionIdToUse))
     {
         eventData.SessionID = sessionIdToUse;
     }
     return(eventData);
 }
 /// <summary>
 /// Writes the value of an object.
 /// </summary>
 /// <param name="o">The object to write.</param>
 public sealed override void WriteLine(object o)
 {
     if (o is Exception)
     {
         TraceEventData eventData = AddCustomerIdAndSessionIdToEventDataIfTheyAreSet(new TraceEventData(TraceEventType.Error));
         Trace(eventData, o);
     }
     else
     {
         TraceEventData eventData = AddCustomerIdAndSessionIdToEventDataIfTheyAreSet(new TraceEventData());
         Trace(eventData, o);
     }
 }
        /// <summary>
        /// Writes trace information and a data object to the listener specific
        /// output.
        /// </summary>
        /// <param name="e">
        /// A <see cref="TraceEventData"/> object that contains information about
        /// the trace event.
        /// </param>
        /// <param name="data">The trace data to write.</param>
        protected virtual void TraceInternal(TraceEventData e, object data)
        {
            string message;

            if (data is object[])
            {
                message = ConvertDataToString((object[])data);
            }
            else
            {
                message = data.ToString();
            }
            TraceInternal(e, message);
        }
        /// <summary>
        /// Writes trace information and a message to Logstash.
        /// </summary>
        /// <param name="e">
        /// A <see cref="TraceListenerBase.TraceEventData"/> object that contains
        /// information about the trace event.
        /// </param>
        /// <param name="message">The message to write.</param>
        protected override void TraceInternal(TraceEventData e, string message)
        {
            var logEntry = new LogEntry
            {
                Level     = e.Type.ToLogLevel(),
                Source    = e.Source,
                Message   = message,
                EventId   = e.ID,
                ProcessId = e.Cache.ProcessId,
                ThreadId  = e.Cache.ThreadId
            };

            WriteLogEntry(logEntry);
        }
 public List <TraceEventData> CreateEventKeys(NameValueCollection keys)
 {
     if (keys == null || keys.Count == 0)
     {
         return((List <TraceEventData>)null);
     }
     return(((IEnumerable <string>)keys.AllKeys).Where <string>((Func <string, bool>)(key => !string.IsNullOrWhiteSpace(key))).Select <string, TraceEventData>((Func <string, TraceEventData>)(key =>
     {
         TraceEventData traceEventData = new TraceEventData();
         traceEventData.EventDataType = key;
         string str = keys[key];
         traceEventData.EventDataBody = str;
         return traceEventData;
     })).ToList <TraceEventData>());
 }
        /// <summary>
        /// Enqueues trace information and a message to the queue for
        /// asynchronous processing.
        /// </summary>
        /// <param name="e">
        /// A <see cref="TraceListenerBase.TraceEventData"/> object that contains
        /// information about the trace event.
        /// </param>
        /// <param name="message">The message to write.</param>
        protected override void TraceInternal(TraceEventData e, string message)
        {
            var logEntry = new LogEntry
            {
                Level      = e.Type.ToLogLevel(),
                Source     = e.Source,
                Message    = message,
                EventId    = e.ID,
                ProcessId  = e.Cache.ProcessId,
                ThreadId   = e.Cache.ThreadId,
                CustomerId = e.CustomerID,
                SessionId  = e.SessionID
            };

            RequestQueue.Enqueue(logEntry);
        }
        /// <summary>
        /// Enqueues trace information and exception data to the queue for
        /// asynchronous processing.
        /// </summary>
        /// <param name="e">
        /// A <see cref="TraceListenerBase.TraceEventData"/> object that contains
        /// information about the trace event.
        /// </param>
        /// <param name="data">The log data to write.</param>
        protected void TraceInternal(TraceEventData e, LogData data)
        {
            var logEntry = new LogEntry
            {
                Level      = e.Type.ToLogLevel(),
                Source     = e.Source,
                Message    = data.Message,
                Data       = data.Data,
                EventId    = e.ID,
                ProcessId  = e.Cache.ProcessId,
                ThreadId   = e.Cache.ThreadId,
                CustomerId = e.CustomerID,
                SessionId  = e.SessionID
            };

            RequestQueue.Enqueue(logEntry);
        }
        /// <summary>
        /// Enqueues trace information and data to the queue for
        /// asynchronous processing.
        /// </summary>
        /// <param name="e">
        /// A <see cref="TraceListenerBase.TraceEventData"/> object that contains
        /// information about the trace event.
        /// </param>
        /// <param name="data">The trace data to write.</param>
        protected override void TraceInternal(TraceEventData e, object data)
        {
            if (data != null && data is LogData logData)
            {
                TraceInternal(e, logData);
                return;
            }

            var logEntry = new LogEntry
            {
                Level      = e.Type.ToLogLevel(),
                Source     = e.Source,
                EventId    = e.ID,
                ProcessId  = e.Cache.ProcessId,
                ThreadId   = e.Cache.ThreadId,
                CustomerId = e.CustomerID,
                SessionId  = e.SessionID
            };

            if (data is CustomerLogData)
            {
                var content = CustomerLogData.TryExtractCustomerId(data, out Guid? customerId);
                if (content.GetType() == typeof(String) || content.GetType() == typeof(string))
                {
                    logEntry.Message = content.ToString();
                }
                else if (content is LogData dataLogData)
                {
                    logEntry.Message = dataLogData.Message;
                    logEntry.Data    = dataLogData.Data;
                }
                else
                {
                    logEntry.Data = content;
                }
            }
            else
            {
                logEntry.Data = data;
            }

            RequestQueue.Enqueue(logEntry);
        }
        /// <summary>
        /// Writes trace information and a message to standard output.
        /// </summary>
        /// <param name="e">
        /// A <see cref="TraceListenerBase.TraceEventData"/> object that contains
        /// information about the trace event.
        /// </param>
        /// <param name="message">The message to write.</param>
        protected override void TraceInternal(TraceEventData e, string message)
        {
            Console.Write("[");
            using (new ConsoleHighlight(ConsoleColor.Gray))
                Console.Write($"{e.Cache.DateTime:T}");
            Console.Write("] ");

            Console.Write("[");
            using (FormatForLogLevel(e.Type ?? TraceEventType.Information))
                Console.Write($"{e.Type ?? TraceEventType.Information}");
            Console.Write("] ");

            Console.Write(message);

            Console.Write(" [");
            using (new ConsoleHighlight(ConsoleColor.DarkCyan))
                Console.Write($"{e.Source}");
            Console.Write("]");

            Console.WriteLine();
        }
        public List <TraceEventData> CreateTraceEventData(Exception exception, IEnumerable <KeyValuePair <string, object> > blobsWithLabels)
        {
            if (exception == null && blobsWithLabels == null)
            {
                return((List <TraceEventData>)null);
            }
            List <TraceEventData> traceEventDataList = new List <TraceEventData>();
            TraceEventData        traceEventData     = this.ToTraceEventData(exception);

            if (traceEventData != null)
            {
                traceEventDataList.Add(traceEventData);
            }
            if (!string.IsNullOrWhiteSpace(exception != null ? exception.StackTrace : (string)null))
            {
                traceEventDataList.Add(new TraceEventData()
                {
                    EventDataBody = exception.StackTrace,
                    EventDataType = "StackTrace"
                });
            }
            if (blobsWithLabels != null)
            {
                traceEventDataList.AddRange(blobsWithLabels.Where <KeyValuePair <string, object> >((Func <KeyValuePair <string, object>, bool>)(kv =>
                {
                    if (!string.IsNullOrWhiteSpace(kv.Key))
                    {
                        return(kv.Value != null);
                    }
                    return(false);
                })).Select <KeyValuePair <string, object>, TraceEventData>((Func <KeyValuePair <string, object>, TraceEventData>)(kv => new TraceEventData()
                {
                    EventDataBody = this._serializer.Serialize <object>(kv.Value),
                    EventDataType = kv.Key
                })).Take <TraceEventData>(16));
            }
            return(traceEventDataList);
        }
        /// <summary>
        /// Writes trace information, a data object and event information to the
        /// listener specific output.
        /// </summary>
        /// <param name="eventCache">
        /// A <see cref="T:System.Diagnostics.TraceEventCache"/> object that
        /// contains the current process ID, thread ID, and stack trace
        /// information.
        /// </param>
        /// <param name="source">
        /// A name used to identify the output, typically the name of the
        /// application that generated the trace event.
        /// </param>
        /// <param name="eventType">
        /// One of the <see cref="T:System.Diagnostics.TraceEventType"/> values
        /// specifying the type of event that has caused the trace.
        /// </param>
        /// <param name="id">A numeric identifier for the event.</param>
        /// <param name="data">The trace data to emit.</param>
        public sealed override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
        {
            TraceEventData eventData;

            if (data != null && data is CustomerLogData customerData)
            {
                if (customerData.HasCustomerId() && customerData.HasSessionId())
                {
                    eventData = new TraceEventData(eventCache, source, eventType, id, customerData.CustomerId, customerData.SessionId);
                }
                else if (customerData.HasCustomerId())
                {
                    eventData = new TraceEventData(eventCache, source, eventType, id, customerData.CustomerId);
                    if (ADefaultSessionIdIsSet(out string sessionIdToUse))
                    {
                        eventData.SessionID = sessionIdToUse;
                    }
                }
                else if (customerData.HasSessionId())
                {
                    eventData = new TraceEventData(eventCache, source, eventType, id, null, customerData.SessionId);
                    if (ADefaultCustomerIdIsSet(out Guid customerIdToUse))
                    {
                        eventData.CustomerID = customerIdToUse;
                    }
                }
                else
                {
                    eventData = AddCustomerIdAndSessionIdToEventDataIfTheyAreSet(new TraceEventData(eventCache, source, eventType, id));
                }
                Trace(eventData, data);
            }
            else if (data != null)
            {
                eventData = AddCustomerIdAndSessionIdToEventDataIfTheyAreSet(new TraceEventData(eventCache, source, eventType, id));
                Trace(eventData, data);
            }
        }
        /// <summary>
        /// Writes trace information, a formatted array of objects and event
        /// information to the listener specific output.
        /// </summary>
        /// <param name="eventCache">
        /// A <see cref="T:System.Diagnostics.TraceEventCache"/> object that
        /// contains the current process ID, thread ID, and stack trace
        /// information.
        /// </param>
        /// <param name="source">
        /// A name used to identify the output, typically the name of the
        /// application that generated the trace event.
        /// </param>
        /// <param name="eventType">
        /// One of the <see cref="T:System.Diagnostics.TraceEventType"/> values
        /// specifying the type of event that has caused the trace.
        /// </param>
        /// <param name="id">A numeric identifier for the event.</param>
        /// <param name="format">
        /// A format string that contains zero or more format items, which
        /// correspond to objects in the <paramref name="args"/> array.
        /// </param>
        /// <param name="args">
        /// An object array containing zero or more objects to format.
        /// </param>
        public sealed override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
        {
            TraceEventData eventData = AddCustomerIdAndSessionIdToEventDataIfTheyAreSet(new TraceEventData(eventCache, source, eventType, id));

            Trace(eventData, format, args);
        }
 protected override void TraceInternal(TraceEventData e, object data)
 {
     TraceCalled = true;
     Invocations.Add(data);
     base.TraceInternal(e, data);
 }
 protected override void TraceInternal(TraceEventData e, string message)
 {
     TraceCalled = true;
     Invocations.Add(message);
     base.TraceInternal(e, message);
 }
        /// <summary>
        /// Writes trace information, a message, and event information to the
        /// listener specific output.
        /// </summary>
        /// <param name="eventCache">
        /// A <see cref="T:System.Diagnostics.TraceEventCache"/> object that
        /// contains the current process ID, thread ID, and stack trace
        /// information.
        /// </param>
        /// <param name="source">
        /// A name used to identify the output, typically the name of the
        /// application that generated the trace event.
        /// </param>
        /// <param name="eventType">
        /// One of the <see cref="T:System.Diagnostics.TraceEventType"/> values
        /// specifying the type of event that has caused the trace.
        /// </param>
        /// <param name="id">A numeric identifier for the event.</param>
        /// <param name="message">A message to write.</param>
        public sealed override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message)
        {
            TraceEventData eventData = AddCustomerIdAndSessionIdToEventDataIfTheyAreSet(new TraceEventData(eventCache, source, eventType, id));

            Trace(eventData, message);
        }
        /// <summary>
        /// Writes a message.
        /// </summary>
        /// <param name="message">The message to write.</param>
        public sealed override void WriteLine(string message)
        {
            TraceEventData eventData = AddCustomerIdAndSessionIdToEventDataIfTheyAreSet(new TraceEventData());

            Trace(eventData, message);
        }
        /// <summary>
        /// Writes a data object, using the category as event type.
        /// </summary>
        /// <param name="o">The object to write.</param>
        /// <param name="category">The category to use.</param>
        public sealed override void WriteLine(object o, string category)
        {
            TraceEventData eventData = AddCustomerIdAndSessionIdToEventDataIfTheyAreSet(new TraceEventData(ParseCategory(category)));

            Trace(eventData, o);
        }
 /// <summary>
 /// Writes trace information and a formatted message to the listener
 /// specific output.
 /// </summary>
 /// <param name="e">
 /// A <see cref="TraceEventData"/> object that contains information about
 /// the trace event.
 /// </param>
 /// <param name="format">
 /// A format string that contains zero or more format items, which
 /// correspond to objects in the <paramref name="args"/> array.
 /// </param>
 /// <param name="args">
 /// An object array containing zero or more objects to format.
 /// </param>
 protected virtual void TraceInternal(TraceEventData e, string format, params object[] args) => TraceInternal(e, string.Format(format, args));
 /// <summary>
 /// Sends trace information and an object to remote host.
 /// </summary>
 /// <param name="e">
 /// A <see cref="TraceListenerBase.TraceEventData"/> object that contains
 /// information about the trace event.
 /// </param>
 /// <param name="data">The object to write.</param>
 protected override void TraceInternal(TraceEventData e, object data)
 {
     TraceInternal(e, data.ToString());
 }
 /// <summary>
 /// When overridden in a derived class, writes trace information and a
 /// message to the listener specific output.
 /// </summary>
 /// <param name="e">
 /// A <see cref="TraceEventData"/> object that contains information about
 /// the trace event.
 /// </param>
 /// <param name="message">The message to write.</param>
 protected abstract void TraceInternal(TraceEventData e, string message);
 /// <summary>
 /// Sends trace information and a message to remote host.
 /// </summary>
 /// <param name="e">
 /// A <see cref="TraceListenerBase.TraceEventData"/> object that contains
 /// information about the trace event.
 /// </param>
 /// <param name="message">The message to write.</param>
 protected override void TraceInternal(TraceEventData e, string message)
 {
     Send(new LogForUdp(ConstructLogMessageToWrite(e, message), GetTraceEventDataTypeWithInformationAsDefault(e)));
 }