Beispiel #1
0
        void LogMessage(LogLevel level, string format, params object[] args)
        {
            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }

            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            if (!ShouldLog(level))
            {
                return;
            }

            var msg   = args.Length != 0 ? string.Format(format, args) : format;
            var now   = DateTime.Now;
            var stamp = TimestampFormat != string.Empty ?
                        now.ToString(TimestampFormat) : null;

            lock (_lock)
            {
                MessageLogged?.Invoke(this, new LogEventArgs(level, now, msg));

                foreach (var logger in Loggers)
                {
                    logger.Log(level, stamp, Source, Category, msg);
                }
            }
        }
Beispiel #2
0
            public void Log(object sender, MessageLoggedEventArgs e)
            {
                MessageLogged?.Invoke(sender, e);

                var level = e.Severity switch
                {
                    LogMessageSeverity.Trace => LogLevel.Trace,
                    LogMessageSeverity.Debug => LogLevel.Debug,
                    LogMessageSeverity.Information => LogLevel.Information,
                    LogMessageSeverity.Warning => LogLevel.Warning,
                    LogMessageSeverity.Error => LogLevel.Error,
                    LogMessageSeverity.Critical => LogLevel.Critical,

                    _ => LogLevel.None
                };

                // downgrade unknown dispatch logs to debug level
                if (e.Source.Equals("Gateway", StringComparison.OrdinalIgnoreCase) && e.Message.Contains("Unknown dispatch", StringComparison.OrdinalIgnoreCase))
                {
                    level = level < LogLevel.Debug ? level : LogLevel.Debug;
                }

                _logger.Log(level, e.Exception, $"[{e.Source}] {e.Message}");

                // log a note about expired tokens: https://github.com/chiyadev/MudaeFarm/issues/153
                if (e.Message.Contains("AuthenticationFailed", StringComparison.OrdinalIgnoreCase))
                {
                    _logger.Log(level, "Your Discord authentication token seems to have expired. Please try updating your token in \"profiles.json\" file in the folder \"%localappdata%\\MudaeFarm\".");
                }
            }
        }
 public static void Dispatch(string level, string message)
 {
     if (MessageLogged != null)
     {
         MessageLogged.Invoke(null, new LogMessageEventArgs(level, message));
     }
 }
 internal void RaiseMessageLoggedEvent(MediaLogMessage e)
 {
     GuiContext.Current.EnqueueInvoke(() =>
     {
         MessageLogged?.Invoke(this, new MediaLogMessageEventArgs(e));
     });
 }
Beispiel #5
0
        private static void LogMessage(LogObject logObject)
        {
            _log.Add(logObject);

            var message = logObject.ToString();

            if (logObject.Type == MessageType.Error)
            {
                Trace.TraceError(message);
            }
            else if (logObject.Type == MessageType.Warning)
            {
                Trace.TraceWarning(message);
            }
            else
            {
                Trace.TraceInformation(message);
            }

            if (Out != null)
            {
                try
                {
                    Out.WriteLine(message);
                    Out.Flush();
                }
                catch (Exception e)
                {
                    Trace.TraceError("Unable to write to the log output stream: {0}", e);
                    Out = null;
                }
            }

            MessageLogged?.Invoke(logObject);
        }
Beispiel #6
0
        public void Log(string text, LogState state = LogState.None)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return;
            }

            lock (Logs)
            {
                var log = new Log()
                {
                    Date    = DateTime.Now,
                    Message = text,
                    State   = state
                };

                if (Logs.Count >= 100)
                {
                    Logs.RemoveRange(0, Logs.Count - 100);
                }

                Logs.Add(log);
                MessageLogged?.Invoke();
            }
        }
Beispiel #7
0
            public void Log(object sender, MessageLoggedEventArgs e)
            {
                MessageLogged?.Invoke(sender, e);

                var level = e.Severity switch
                {
                    LogMessageSeverity.Trace => LogLevel.Trace,
                    LogMessageSeverity.Debug => LogLevel.Debug,
                    LogMessageSeverity.Information => LogLevel.Information,
                    LogMessageSeverity.Warning => LogLevel.Warning,
                    LogMessageSeverity.Error => LogLevel.Error,
                    LogMessageSeverity.Critical => LogLevel.Critical,

                    _ => LogLevel.None
                };

                // downgrade unknown dispatch logs to debug level
                if (e.Source.Equals("Gateway", StringComparison.OrdinalIgnoreCase) && e.Message.Contains("Unknown dispatch", StringComparison.OrdinalIgnoreCase))
                {
                    level = level < LogLevel.Debug ? level : LogLevel.Debug;
                }

                _logger.Log(level, e.Exception, $"[{e.Source}] {e.Message}");
            }
        }
Beispiel #8
0
        public static void Write(string message, string category)
        {
            string str = "[" + DateTime.Now.ToString(@"HH:mm:ss") + "][" + category + "] " + message;

            Console.WriteLine(str);
            MessageLogged?.Invoke(str);
        }
 internal void OnMessageLogged(ILogMessage msg)
 {
     if (this.MessageLogged != null)
     {
         MessageLogged?.Invoke(msg);
     }
 }
Beispiel #10
0
        public void Log(string message)
        {
            var logMessage = $"[{DateTime.Now}] {message}\n";

            MessageLogged?.Invoke(logMessage);
            Console.Write(logMessage);

            _allLogs.Append(logMessage);
        }
Beispiel #11
0
        /// <summary>
        /// Invokes the MessageLogged event with the new event from the legacy TraceEvent system.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="eventType"></param>
        /// <param name="id"></param>
        /// <param name="text"></param>
        public override void TraceEvent(string source, LogEventType eventType, int id, string text)
        {
            Event @event = new Event(0, (int)eventType, text, source, 0);

            using (EventCollection eventCollection = new EventCollection(new Event[] { @event }))
            {
                MessageLogged.Invoke(eventCollection);
            }
        }
        protected virtual void Log(MessageLoggedEventArgs e)
        {
            if (e.IsError)
            {
                HasLoggedErrors = true;
            }

            MessageLogged?.Invoke(this, e);
        }
        public void Log(VerboseLevels verbosity, string message)
        {
            if (verbosity > Verbosity)
            {
                return;
            }

            MessageLogged?.Invoke(this, new SapLogEventArgs(verbosity, message));
        }
Beispiel #14
0
        public static void Info(string message)
        {
            MessageLogged?.Invoke(LogType.Info, message);
            ConsoleColor originalColor = Console.ForegroundColor;

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(GetPrefix() + message);
            Console.ForegroundColor = originalColor;
        }
Beispiel #15
0
        public static void Warning(object message)
        {
            MessageLogged?.Invoke(LogType.Warning, message.ToString());
            ConsoleColor originalColor = Console.ForegroundColor;

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(message);
            Console.ForegroundColor = originalColor;
        }
Beispiel #16
0
        public static void Success(object message)
        {
            MessageLogged?.Invoke(LogType.Success, message.ToString());
            ConsoleColor originalColor = Console.ForegroundColor;

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(message);
            Console.ForegroundColor = originalColor;
        }
Beispiel #17
0
        public static void Error(object message)
        {
            MessageLogged?.Invoke(LogType.Error, message.ToString());
            ConsoleColor originalColor = Console.ForegroundColor;

            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(message);
            Console.ForegroundColor = originalColor;
        }
Beispiel #18
0
 internal void RaiseMessageLoggedEvent(MediaLogMessage e)
 {
     WindowsPlatform.Instance.Gui?.EnqueueInvoke(DispatcherPriority.Background,
                                                 new Action <MediaLogMessage>((eventArgs) =>
     {
         MessageLogged?.Invoke(this, new MediaLogMessageEventArgs(eventArgs));
     }),
                                                 e);
 }
Beispiel #19
0
        public static void Success(string message)
        {
            MessageLogged?.Invoke(LogType.Success, message);
            ConsoleColor originalColor = Console.ForegroundColor;

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(GetPrefix() + message);
            Console.ForegroundColor = originalColor;
        }
Beispiel #20
0
        public static void Warning(string message)
        {
            MessageLogged?.Invoke(LogType.Warning, message);
            ConsoleColor originalColor = Console.ForegroundColor;

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(GetPrefix() + message);
            Console.ForegroundColor = originalColor;
        }
Beispiel #21
0
        public static void ColoredLog(object message, ConsoleColor color)
        {
            MessageLogged?.Invoke(LogType.Info, message.ToString());
            ConsoleColor originalColor = Console.ForegroundColor;

            Console.ForegroundColor = color;
            Console.WriteLine(message);
            Console.ForegroundColor = originalColor;
        }
Beispiel #22
0
        public static void Error(string message)
        {
            WriteToLog(LogType.Error, message);
            MessageLogged?.Invoke(LogType.Error, message);
            ConsoleColor originalColor = Console.ForegroundColor;

            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(GetPrefix() + message);
            Console.ForegroundColor = originalColor;
        }
Beispiel #23
0
        public static void Debug(object message)
        {
#if DEBUG
            MessageLogged?.Invoke(LogType.Debug, message.ToString());
            ConsoleColor originalColor = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine(message);
            Console.ForegroundColor = originalColor;
#endif
        }
 static public void LogMessage(string msg)
 {
     Console.WriteLine(msg);
     LogMessages.Enqueue(msg);
     while (LogMessages.Count > 200)
     {
         LogMessages.TryDequeue(out _);
     }
     MessageLogged?.Invoke(msg);
 }
Beispiel #25
0
 protected void OnMessageLogged(MessageLevel level, string?message, Exception?error)
 {
     try
     {
         MessageLogged?.Invoke(level, message, error);
     }
     catch
     {
     }
 }
Beispiel #26
0
 public static void Debug(string message)
 {
     if (DEBUG)
     {
         MessageLogged?.Invoke(LogType.Debug, message);
         ConsoleColor originalColor = Console.ForegroundColor;
         Console.ForegroundColor = ConsoleColor.Blue;
         Console.WriteLine(GetPrefix() + message);
         Console.ForegroundColor = originalColor;
     }
 }
Beispiel #27
0
        /// <summary>
        /// Trigger <see cref="MessageLogged"/>, and stop weaving if
        /// <paramref name="importance"/> is <see cref="MessageImportance.Error"/>.
        /// </summary>
        private void LogMessage(object sender, string msg, MessageImportance importance)
        {
            bool willThrow = importance == MessageImportance.Error ||
                             (importance == MessageImportance.Warning && TreatWarningsAsErrors);

            MessageLogged?.Invoke(new MessageLoggedEventArgs(msg, importance, willThrow, BeingProcessed, sender.GetType()));

            if (willThrow)
            {
                throw new WeavingException(msg, BeingProcessed, sender.GetType());
            }
        }
Beispiel #28
0
 /// <summary>
 /// Logs the specified message.
 /// </summary>
 /// <param name="msg">The MSG.</param>
 public static void LogMessage(string msg, LogAction action = LogAction.LogOnly)
 {
     try
     {
         Log.Information(msg);
         if (action == LogAction.LogAndShow)
         {
             MessageLogged?.Invoke(null, DateTime.Now + " : " + msg);
         }
     }
     catch (Exception)
     { }
 }
        public void Log(string message)
        {
            if (string.IsNullOrWhiteSpace(message))
            {
                throw new ArgumentNullException();
            }

            LastMessage = message;

            // Write the log to a storage
            // ...

            MessageLogged?.Invoke(this, DateTime.UtcNow);
        }
Beispiel #30
0
        /// <summary>
        /// Write a message and possibly an exception to the logging system
        /// </summary>
        /// <param name="loggerType">Type of logger to use</param>
        /// <param name="logLevel">Level of log reporting</param>
        /// <param name="message">Message to be logged</param>
        /// <param name="ex">Exception to be logged; null if no exception</param>
        /// <remarks>Log message will not be written if logLevel is LogLevel or higher)</remarks>
        private static void WriteLogWork(LoggerTypes loggerType, BaseLogger.LogLevels logLevel, string message, Exception ex)
        {
            if (OfflineMode && loggerType == LoggerTypes.LogDb)
            {
                loggerType = LoggerTypes.LogFile;
            }

            BaseLogger myLogger;

            // Establish which logger will be used
            switch (loggerType)
            {
            case LoggerTypes.LogDb:
                // Note that the Database logger will (by default) also echo messages to the file logger
                myLogger = mDbLogger;
                message  = System.Net.Dns.GetHostName() + ": " + message;
                break;

            case LoggerTypes.LogFile:
                myLogger = mFileLogger;

                if (!string.IsNullOrWhiteSpace(FileLogger.LogFilePath) &&
                    !FileLogger.LogFilePath.Contains(Path.DirectorySeparatorChar.ToString()))
                {
                    var    logFileName = Path.GetFileName(FileLogger.LogFilePath);
                    string workDirLogPath;
                    if (string.IsNullOrEmpty(WorkDirPath))
                    {
                        workDirLogPath = Path.Combine(".", logFileName);
                    }
                    else
                    {
                        workDirLogPath = Path.Combine(WorkDirPath, logFileName);
                    }

                    ChangeLogFileBaseName(workDirLogPath, FileLogger.AppendDateToBaseFileName);
                }

                break;

            default:
                throw new Exception("Invalid logger type specified");
            }

            MessageLogged?.Invoke(message, logLevel);

            // Send the log message
            myLogger?.LogMessage(logLevel, message, ex);
        }