Exemplo n.º 1
0
        private async Task SendData(TableInfo table, LogEntity logEntity)
        {
            while (true)
            {
                await _lock.WaitAsync();

                try
                {
                    if (!_isConnect || _writer == null)
                    {
                        _writer?.Dispose();
                        _client?.Dispose();

                        _client    = new TcpClient(_settings.LogStash.Host, _settings.LogStash.Port);
                        _writer    = new StreamWriter(_client.GetStream());
                        _isConnect = true;
                    }

                    var dto = new LogDto()
                    {
                        DateTime    = logEntity.DateTime,
                        Level       = logEntity.Level,
                        Version     = logEntity.Version,
                        Component   = logEntity.Component,
                        Process     = logEntity.Process,
                        Context     = logEntity.Context,
                        Type        = logEntity.Type,
                        Stack       = logEntity.Stack,
                        Msg         = logEntity.Msg,
                        Table       = table.Name,
                        AccountName = table.Account
                    };
                    PreparingContext(dto);

                    var json = dto.ToJson();

                    await _writer.WriteLineAsync(json);

                    return;
                }
                catch (Exception ex)
                {
                    await _log.WriteInfoAsync(nameof(AzureLogHandler), nameof(SendData), $"{_settings.LogStash.Host}:_settings.LogStash.Port", ex.ToString());

                    await Task.Delay(2000);

                    _isConnect = false;
                }
                finally
                {
                    _lock.Release();
                }
            }
        }
Exemplo n.º 2
0
        private async Task <bool> SendDataToAggregator(TableInfo table, IEnumerable <LogEntity> logEntityList)
        {
            var data = logEntityList.Select(logEntity =>
            {
                var dto = new LogDto()
                {
                    DateTime    = logEntity.DateTime,
                    Level       = logEntity.Level,
                    Version     = logEntity.Version,
                    Component   = logEntity.Component,
                    Process     = logEntity.Process,
                    Context     = logEntity.Context,
                    Type        = logEntity.Type,
                    Stack       = logEntity.Stack,
                    Msg         = logEntity.Msg,
                    Table       = table.Name,
                    AccountName = table.Account,
                    Env         = logEntity.Env
                };
                PreparingContext(dto);

                var json = dto.ToJson();

                return(new
                {
                    topic = "lykke-logs",
                    sender = $"{table.Account}.{table.Name}",
                    level = logEntity.Level,
                    document = json
                });
            })
                       .ToList();



            try
            {
                var result = await _aggregatorUrl.PostJsonAsync(data);

                return(result.StatusCode == HttpStatusCode.OK);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Cannot sent logs to aggregator: {ex}");
                return(false);
            }
        }
        private void PreparingContext(LogDto logEntity)
        {
            if (!_settings.ParseContextAsJson && (string.IsNullOrEmpty(logEntity.Context) || !logEntity.Context.StartsWith('{')))
            {
                return;
            }

            try
            {
                var ctx = JObject.Parse(logEntity.Context);
                logEntity.ContextData = ctx;
            }
            // ReSharper disable once EmptyGeneralCatchClause
            catch (Exception)
            {
            }
        }
        private async Task SendDataToSocket(StreamWriter writer, TableInfo table, LogEntity logEntity)
        {
            var dto = new LogDto()
            {
                DateTime    = logEntity.DateTime,
                Level       = logEntity.Level,
                Version     = logEntity.Version,
                Component   = logEntity.Component,
                Process     = logEntity.Process,
                Context     = logEntity.Context,
                Type        = logEntity.Type,
                Stack       = logEntity.Stack,
                Msg         = logEntity.Msg,
                Table       = table.Name,
                AccountName = table.Account,
                Env         = logEntity.Env
            };

            PreparingContext(dto);

            var json = dto.ToJson();

            await writer.WriteLineAsync(json);
        }