public override void Log <TState>(
            LogLevel logLevel,
            EventId eventId,
            TState state,
            Exception?exception,
            Func <TState, Exception?, string> formatter)
        {
            string line = $"{logLevel,-12}";

            lock (ScopeStringsStack)
            {
                foreach (var scopeString in ScopeStringsStack.Reverse())
                {
                    line += scopeString + @" -> ";
                }
            }
            line += formatter(state, exception);
            Exception?ex = exception;

            if (ex is not null)
            {
                line += "\tException: " + ex.Message;
            }

            _writeLineAction(line);
        }
Esempio n. 2
0
        public override void Log <TState>(
            LogLevel logLevel,
            EventId eventId,
            TState state,
            Exception?exception,
            Func <TState, Exception?, string> formatter)
        {
            if (!IsEnabled(logLevel))
            {
                return;
            }

            string line1;

            if (eventId.Id != 0)
            {
                line1 = $"[ {eventId.Id}: {logLevel,-11} ]";
            }
            else
            {
                line1 = $"[ {logLevel,-11} ]";
            }

            string line2 = "\t";

            lock (ScopeStringsStack)
            {
                foreach (var scopeString in ScopeStringsStack.Reverse())
                {
                    line2 += scopeString + @" -> ";
                }
            }
            line2 += formatter(state, exception);
            Exception?ex = exception;

            while (ex is not null)
            {
                line2 += "\n";
                line2 += "\tException: ";
                line2 += ex.Message;

                line2 += "\n";
                line2 += "\tStackTrace: ";
                line2 += ex.StackTrace;

                ex = ex.InnerException;
            }

            if (Options.DuplicateInConsole)
            {
                ConsoleColor originalColor = Console.ForegroundColor;

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine(line1);

                Console.ForegroundColor = originalColor;
                Console.WriteLine(line2);
            }
            lock (_logFileTextWriter)
            {
                _logFileTextWriter.WriteLine(line1);

                _logFileTextWriter.WriteLine(line2);
            }
        }