Exemple #1
0
        private void Start()
        {
            if (_isStarted)
            {
                return;
            }

            lock (_lockObject)
            {
                if (_isStarted)
                {
                    return;
                }

                _isStarted = true;
            }

            if (_systemLogService == null)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.Write($"DEBUG\tApplication started without send logs to the remote server");
                Console.ForegroundColor = ConsoleColor.White;
            }

            _threadTimer.Register("SystemLog Pusher", async() =>
            {
                var eventsToPush = GetEventsToPush();

                if (eventsToPush == null)
                {
                    return;
                }

                foreach (var log in eventsToPush)
                {
                    Console.ForegroundColor =
                        log.LogLevel switch
                    {
                        LogLevel.Info => ConsoleColor.Green,
                        LogLevel.Warning => ConsoleColor.Yellow,
                        _ => ConsoleColor.Red
                    };
                    Console.Write(log.LogLevel);
                    Console.ForegroundColor = ConsoleColor.White;

                    Console.WriteLine($"\t{log.DateTime: HH:mm:ss:fff} {log.Component}; {log.Process}");
                    Console.WriteLine($"\t{log.Message}");
                    if (!string.IsNullOrEmpty(log.StackTrace))
                    {
                        Console.WriteLine(log.StackTrace);
                    }

                    log.AppName    = _appName;
                    log.AppVersion = _appVersion;
                }

                try
                {
                    if (_systemLogService != null)
                    {
                        await _systemLogService.RegisterAsync(new LogEventRequest
                        {
                            Component = _appName,
                            Events    = eventsToPush
                        });
                    }
                }
                catch (Exception ex)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("CRITICAL");
                    Console.WriteLine($"\t{DateTime.UtcNow:HH:mm:ss.mmm} Cannot send logs to remote server. Count: {_events.Count}");
                    Console.WriteLine(ex.ToString());
                    Console.ForegroundColor = ConsoleColor.White;
                }
            });