예제 #1
0
        void Log(CoapNetLogMessageLevel level, string source, Exception exception, string message, params object[] parameters)
        {
            if (_sinksCount == 0)
            {
                return; // Ensure that performance is not decreased when nothing is logged.
            }

            var logMessage = new CoapNetLogMessage
            {
                Timestamp  = DateTime.UtcNow, // Use UTC for performance reasons.
                Source     = source,
                Level      = level,
                Exception  = exception,
                Message    = message,
                Parameters = parameters,
            };

            lock (_sinks)
            {
                foreach (var sink in _sinks)
                {
                    sink.ProcessLogMessage(logMessage);
                }
            }
        }
예제 #2
0
        public void ProcessLogMessage(CoapNetLogMessage logMessage)
        {
            if (logMessage is null)
            {
                throw new ArgumentNullException(nameof(logMessage));
            }

            var formattedMessage = logMessage.Message;

            if (logMessage.Parameters != null)
            {
                formattedMessage = string.Format(logMessage.Message, logMessage.Parameters);
            }

            Console.WriteLine($"[{logMessage.Timestamp.ToString()}] [{logMessage.ThreadId}] [{logMessage.Level}] [{formattedMessage}]");
            if (logMessage.Exception != null)
            {
                Console.WriteLine("[\r\n" + logMessage.Exception + "\r\n]");
            }
        }