Exemplo n.º 1
0
        private static List <IInterceptor> BuildStandardInterceptors(object instance, ILogger logger, LogType logType)
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }
            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            var interceptors = new List <IInterceptor>();

            if (logType.HasFlag(LogType.Tracking))
            {
                interceptors.Add(new AsyncTrackingInterceptor().ToInterceptor());
            }

            if (logType.HasFlag(LogType.Error))
            {
                interceptors.Add(new AsyncErrorLoggingInterceptor(logger).ToInterceptor());
            }

            if (logType.HasFlag(LogType.Performance))
            {
                interceptors.Add(new AsyncPerformanceLoggingInterceptor(logger).ToInterceptor());
            }

            if (logType.HasFlag(LogType.Diagnostic))
            {
                interceptors.Add(new AsyncDiagnosticLoggingInterceptor(logger, FilterTheseParameters).ToInterceptor());
            }

            return(interceptors);
        }
Exemplo n.º 2
0
 private void Write(LogType type, LogEventLevel level, Exception?ex, string message, params object?[] items)
 {
     if (type.HasFlag(LogType.Screen))
     {
         if (_screenLogger != null && _levelSwitch.MinimumLevel >= LogEventLevel.Information)
         {
             _screenLogger.Write(level, ex, message, items);
         }
         else if (_debugScreenLogger != null)
         {
             _debugScreenLogger.Write(level, ex, message, items);
         }
         if (_notificationLogger != null)
         {
             _notificationLogger.Write(level, ex, message, items);
         }
     }
     if (_eventLogger != null && type.HasFlag(LogType.Event))
     {
         _eventLogger.Write(level, ex, message, items);
     }
     if (_diskLogger != null && type.HasFlag(LogType.Disk))
     {
         _diskLogger.Write(level, ex, message, items);
     }
 }
Exemplo n.º 3
0
 public void Info(params object[] args)
 {
     if (_types.HasFlag(LogType.Info))
     {
         Append(LogType.Info, args);
     }
 }
Exemplo n.º 4
0
 private void Write(LogType type, LogEventLevel level, Exception ex, string message, params object[] items)
 {
     if (type.HasFlag(LogType.Screen))
     {
         _screenLogger.Write(level, ex, message, items);
     }
     if (type.HasFlag(LogType.Event))
     {
         _eventLogger.Write(level, ex, message, items);
     }
 }
Exemplo n.º 5
0
 private void Write(LogType type, LogEventLevel level, Exception?ex, string message, params object?[] items)
 {
     if (_screenLogger != null && type.HasFlag(LogType.Screen))
     {
         _screenLogger.Write(level, ex, message, items);
     }
     if (_eventLogger != null && type.HasFlag(LogType.Event))
     {
         _eventLogger.Write(level, ex, message, items);
     }
     if (_diskLogger != null && type.HasFlag(LogType.Disk))
     {
         _diskLogger.Write(level, ex, message, items);
     }
 }
Exemplo n.º 6
0
        public static void WriteLine(string message, LogLevel logLevel = LogLevel.INFO)
        {
#if DEBUG
            Handler = LogType.Console | LogType.File;
#endif
            string msg = "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "] [" + logLevel.ToString() + "] " + message;
            if (Handler.HasFlag(LogType.Console))
            {
                Debug.WriteLine(msg);
            }
            if (Handler.HasFlag(LogType.File))
            {
                if (!initialized)
                {
                    Initialize();
                }
                stream.WriteLine(msg);
                stream.Flush();
            }
        }
Exemplo n.º 7
0
        private static LogItem ToLogItem(this string line, LogType logType)
        {
            if (string.IsNullOrEmpty(line) || line.Length < 2)
            {
                return(null);
            }

            var logTypeStringValue = line.Substring(0, 1);
            var logTypeFromLine    = LogType.None;

            if (!Enum.TryParse <LogType>(logTypeStringValue, out logTypeFromLine))
            {
                return(null);
            }

            if (!logType.HasFlag(logTypeFromLine))
            {
                return(null);
            }

            var dateTimeContentStringValue = line.Substring(2);
            var stringArray = dateTimeContentStringValue.Split(' ');

            if (stringArray == null || stringArray.Length <= 1)
            {
                return(null);
            }

            var dateTimeStringValue = stringArray[0];
            var content             = stringArray[1];
            var dateTime            = DateTime.MinValue;

            if (!DateTime.TryParse(dateTimeStringValue, out dateTime))
            {
                return(null);
            }

            return(new LogItem()
            {
                LogType = logType, Content = content, DateTime = dateTime
            });
        }
Exemplo n.º 8
0
        /// <summary>
        /// Отправить сообщение в журнал.
        /// </summary>
        /// <param name="strText">Текст соообщения</param>
        /// <param name="messageType">Тип сообщения. По умолчанию это Verbose, и если вы ничего не меняли, то префикс для таких сообщений отсутствует</param>
        /// <param name="timeFormat">Формат указания времени сообщения. По умолчанию будет использован формат, переданный LogFile.Init()</param>
        public static void WriteLine(string strText, MessageType messageType = MessageType.Verbose, TimeFormat timeFormat = TimeFormat.Default)
        {
            if (LMessageTypePrefix == null || LTimeFormat == null)
            {
                return;
            }
            if (LMessageTypePrefix.Count == 0 || LTimeFormat.Count == 0)
            {
                return;
            }

            var strTime        = "";
            var strMessageType = LMessageTypePrefix[messageType];
            var tmpTimeFormat  = timeFormat == TimeFormat.Default ? LogFile.timeFormat : timeFormat;

            switch (tmpTimeFormat)
            {
            case TimeFormat.ShortLocalized:
            case TimeFormat.FullLocalized:
                strTime = DateTime.Now.ToString(LTimeFormat[tmpTimeFormat]);
                break;

            case TimeFormat.ShortUTC:
            case TimeFormat.FullUTC:
                strTime = DateTime.UtcNow.ToString(LTimeFormat[tmpTimeFormat]);
                break;
            }

            if (strTime.Length > 0)
            {
                strTime += " ";
            }
            if (strMessageType.Length > 0)
            {
                strMessageType += " ";
            }

            if (logType.HasFlag(LogType.Console))
            {
                Console.WriteLine(strTime + strMessageType + strText);
            }
            if (logType.HasFlag(LogType.Debug))
            {
                System.Diagnostics.Debug.WriteLine(strMessageType + strText);
            }
            if (logType.HasFlag(LogType.File) || logType.HasFlag(LogType.Syslog))
            {
                if (!String.IsNullOrEmpty(strLogFile))
                {
                    var objStream = new StreamWriter(strLogFile, encoding: Encoding.Default, append: true);

                    if (objStream != null)
                    {
                        objStream.WriteLine(strTime + strMessageType + strText);
                        objStream.Flush();
                        objStream.Close();
                        objStream.Dispose();
                        objStream = null;
                    }
                }
            }
        }
Exemplo n.º 9
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="current"></param>
 /// <param name="rule"></param>
 /// <returns></returns>
 public virtual bool IsEnabled(LogType current, LogType rule)
 {
     return(this.LogType.HasFlag(current) && (rule == LogType.All || rule.HasFlag(current)));
 }