Beispiel #1
0
        /// <summary>
        /// Writes a message to the logger
        /// </summary>
        /// <param name="level">Logging level</param>
        /// <param name="format">Message format, compatible with String.Format()</param>
        /// <param name="args">Arguments used to format the message</param>
        public void WriteLine(LogLevel level, string format, params object[] args)
        {
            // ticks to be compared for displaying time diff
            long ticks = DateTime.Now.Ticks;

            // get calling method
            MethodBase method = new StackFrame((int)SkipFrames).GetMethod();
            string     methodName;

            // format method as "<enclosing type>::<method name>"
            if (!ReferenceEquals(null, method))
            {
                methodName = $"{method.DeclaringType.Name}.{method.Name}";
            }
            else
            {
                methodName = "????";
            }

            // format message as "[<milliseconds diff> <level> <method>] <message>"
            string msg = String.Format("[{0:00.0000} {1} {2}] {3}",
                                       TimeSpan.FromTicks(previousTicks == -1 ? 0 : ticks - previousTicks).TotalSeconds, // zero if no previous tick count is set, otherwise the difference of ticks
                                       level.ToShortString(),
                                       methodName,
                                       String.Format(format, args)) + Environment.NewLine;

            // update ticks for next message
            previousTicks = ticks;

            // write message to console
            Console.ForegroundColor = level.GetAssociatedConsoleColor();
            Console.Write(msg);

            // write message to each stream
            this.Streams.ForEach(stream => {
                stream.Write(Encoding.UTF8.GetBytes(msg), 0, msg.Length);
                stream.FlushAsync();
            });
        }
Beispiel #2
0
        /// <summary>
        ///   Logs an object or message with the specified level
        /// </summary>
        /// <param name="level">Logging level</param>
        /// <param name="obj">Object to be logged</param>
        /// <param name="methodName">Method name to be shown</param>
        /// <param name="filePath">File path</param>
        /// <param name="lineNumber">Line number</param>
        private void WriteLine(
            LogLevel level,
            object obj,
            string methodName,
            string filePath,
            int lineNumber)
        {
            // format line
            string line =
                $"{Path.GetFileName(filePath)?.Replace(".cs", ""),25}{(lineNumber != 0 ? ':' + lineNumber.ToString() : ""),-4} {methodName,-15} {level.ToString().ToLower(),7}: {obj}" +
                Environment.NewLine;

            // write to console
            Console.ForegroundColor = level.GetAssociatedConsoleColor();
            Console.Write(line);

            // write to the rest of streams
            byte[] bytes = Encoding.UTF8.GetBytes(line);
            foreach (Stream stream in Streams)
            {
                stream.Write(bytes, 0, bytes.Length);
            }
        }