コード例 #1
0
 private void LogException(Exception ex)
 {
     if (ex == null || ex is AggregateException)
     {
         return;
     }
     using (ConsoleLock.Lock(ConsoleColor.Red)) {
         Console.WriteLine($"STARTUP EXCEPTION: {ex}");
     }
 }
コード例 #2
0
        /// <summary>
        /// Asyncronously logs the log message.
        /// </summary>
        /// <param name="msg">The log message to log.</param>
        /// <param name="logFile">True if the log file should be written to.</param>
        /// <param name="errorFile">True if the error file should be written to.</param>
        public virtual async Task LogAsync(LogMessage msg, bool logFile = true, bool errorFile = false, bool noticeFile = false)
        {
            // Ignore these annoying messages
            if (msg.Exception?.InnerException is WebSocketException ||
                msg.Exception?.InnerException is WebSocketClosedException)
            {
                return;
            }

            if (logFile)
            {
                if (!Directory.Exists(LogDirectory))                     // Create the log directory if it doesn't exist
                {
                    Directory.CreateDirectory(LogDirectory);
                }
                if (!File.Exists(LogFile))                               // Create today's log file if it doesn't exist
                {
                    File.Create(LogFile).Dispose();
                }
            }
            if (errorFile || msg.Exception != null)
            {
                if (!Directory.Exists(ErrorDirectory))                     // Create the error directory if it doesn't exist
                {
                    Directory.CreateDirectory(ErrorDirectory);
                }
                if (!File.Exists(ErrorFile))                               // Create today's error file if it doesn't exist
                {
                    File.Create(ErrorFile).Dispose();
                }
            }
            if (noticeFile)
            {
                if (!Directory.Exists(NoticeDirectory))                     // Create the log directory if it doesn't exist
                {
                    Directory.CreateDirectory(NoticeDirectory);
                }
                if (!File.Exists(NoticeFile))                               // Create today's log file if it doesn't exist
                {
                    File.Create(NoticeFile).Dispose();
                }
            }

            string logText = FormatMessage(msg);

            if (logFile)
            {
                try {
                    File.AppendAllText(LogFile, logText + Environment.NewLine);                         // Write the log text to a file
                } catch (IOException) { }
            }
            if (errorFile || msg.Exception != null)
            {
                try {
                    File.AppendAllText(ErrorFile, logText + Environment.NewLine);                         // Write the error text to a file
                } catch (IOException) { }
            }
            if (noticeFile)
            {
                try {
                    File.AppendAllText(NoticeFile, logText + Environment.NewLine);                         // Write the notice text to a file
                } catch (IOException) { }
            }

            bool shouldPrint = msg.Severity <= LogSeverity.Info || msg.Source == "Command";

            switch (msg.Severity)
            {
            case LogSeverities.Debug: shouldPrint = logDebug; break;

            case LogSeverities.Trace: shouldPrint = logTrace; break;

            case LogSeverities.Print: shouldPrint = logPrint; break;

            case LogSeverities.Notice: shouldPrint = logNotice; break;

            case LogSeverities.Report: shouldPrint = true; break;
            }
            if (shouldPrint)
            {
                // Write the log text to the console
                ConsoleColor?severityColor = GetSeverityColor(msg.Severity);
                if (severityColor.HasValue)
                {
                    using (ConsoleLock.Lock(severityColor))
                        Console.WriteLine(logText);
                }
                else
                {
                    await Console.Out.WriteLineAsync(logText).ConfigureAwait(false);
                }
            }
        }