protected override void Append(LoggingEvent loggingEvent)
        {
            var probasContent = new ProbasContent(loggingEvent, Layout);
            var logs          = new List <LogInfo>
            {
                new LogInfo
                {
                    Time     = DateTimeOffset.Now,
                    Contents = probasContent.Contents
                }
            };
            var tags         = new Dictionary <string, string>();
            var source       = "netapi";
            var logGroupInfo = new LogGroupInfo {
                Logs = logs, LogTags = tags, Topic = Settings.Topic, Source = source
            };
            var request = new PostLogsRequest(Settings.Logstore, logGroupInfo);
            var result  = _client.PostLogStoreLogsAsync(request).Result;

            if (!result.IsSuccess)
            {
                var error = new Exception(result.Error.ToString());
                var msg   = $"post log store logs failed {result.RequestId}";
                ErrorHandler.Error(msg, error);
                Console.WriteLine(msg, error);
            }
            else
            {
                Console.WriteLine($"post log store logs request id {result.RequestId}");
            }
        }
Exemplo n.º 2
0
 public static LogGroup ToProtoModel(this LogGroupInfo domain)
 => domain == null
         ? null
         : new LogGroup
 {
     // https://github.com/aliyun/aliyun-log-dotnetcore-sdk/issues/14
     Topic   = domain.Topic ?? String.Empty,       // Empty is allowed, but not null.
     Source  = domain.Source ?? String.Empty,      // Empty is allowed, but not null.
     LogTags =
     {
         domain.LogTags?
         .Select(x => new LogTag
         {
             Key   = x.Key,
             Value = x.Value ?? String.Empty             // Empty is allowed, but not null.
         })
         ?? Enumerable.Empty <LogTag>()
     },
     Logs =
     {
         domain.Logs?
         .Select(x => x.ToProtoModel())
         ?? Enumerable.Empty <Log>()
     }
 };
        public void Prepare()
        {
            this.client = LogServiceClientBuilders.HttpBuilder
                          .Endpoint("cn-shenzhen.log.aliyuncs.com", "dotnet-sdk-sz")
                          .Credential("<secret>", "<secret>")
                          .Build();

            var timestamp = DateTimeOffset.Now.ToUnixTimeSeconds();

            this.logstoreName = $"logstore-dotnet-sdk-test-{timestamp}";

            // Create logstore for benchmark
            this.client.CreateLogStoreAsync(this.logstoreName, 1, 1).Result.EnsureSuccess();
            Console.WriteLine($"// LogStore {this.logstoreName} created.");

            // Wait logstore prepared
            this.WaitLogStorePrepared().Wait();

            // Prepare data
            var logLines = this.SizeK * 1024 / (10 * LogContentSize) + 1;

            this.logGroup = new LogGroupInfo
            {
                Topic   = "PressureTest",
                Source  = "PressureTest",
                LogTags =
                {
                    { "Tag1", "Value1" },
        public IEnumerable <WeatherForecast> Get()
        {
            var contents = new Dictionary <string, string> {
                { "content", "This is the log write test." }
            };
            var logs = new List <LogInfo>
            {
                new LogInfo
                {
                    Time     = DateTimeOffset.Now,
                    Contents = contents
                }
            };
            var tags         = new Dictionary <string, string>();
            var source       = "netapi";
            var logGroupInfo = new LogGroupInfo {
                Logs = logs, LogTags = tags, Topic = "test-topic", Source = source
            };
            var request = new PostLogsRequest(_options.Logstore, logGroupInfo);
            var result  = _client.PostLogStoreLogsAsync(request).Result;

            var rng = new Random();

            return(Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
                   .ToArray());
        }
 /// <summary>
 /// 向指定的 LogStore 写入日志数据。目前仅支持写入 PB 格式的 <see cref="LogGroupInfo"/> 日志数据。写入时有两种模式:
 /// <list type="bullet">
 ///   <item>
 ///     <description>负载均衡模式(LoadBalance): 自动根据 Logstore 下所有可写的 shard 进行负载均衡写入。该方法对写入可用性较高(SLA: 99.95%),适合写入与消费数据与 shard 无关的场景,例如不保序。</description>
 ///   </item>
 ///   <item>
 ///     <description>根据 Key 路由 shard 模式(KeyHash):写入时需要传递一个 Key,服务端自动根据 Key 选择当前符合该 Key 区间的 Shard 写入。例如,可以将某个生产者(例如 instance)根据名称 Hash 到固定 Shard 上,这样就能保证写入与消费在该 Shard 上是严格有序的(在 Merge/Split 过程中能够严格保证对于 Key 在一个时间点只会出现在一个 Shard 上,参见 <see cref="http://help.aliyun.com/document_detail/28976.html">shard 数据模型</see>)。</description>
 ///   </item>
 /// </list>
 /// </summary>
 /// <param name="client">client实例。</param>
 /// <param name="logstoreName">日志库名称。</param>
 /// <param name="logGroup">一组日志。</param>
 /// <param name="hashKey">(可选)标记日志应该路由到哪个 shard 的标记。</param>
 /// <param name="project">项目名,此参数将覆盖 client 中默认设置。</param>
 /// <returns>异步响应结果。</returns>
 /// <seealso cref="ILogServiceClient.PostLogStoreLogsAsync"/>
 public static Task <IResponse> PostLogStoreLogsAsync(this ILogServiceClient client,
                                                      String logstoreName, LogGroupInfo logGroup,
                                                      String hashKey = null, String project = null)
 => client.PostLogStoreLogsAsync(new PostLogsRequest(logstoreName, logGroup)
 {
     HashKey     = hashKey,
     ProjectName = project
 });
        async protected override Task EmitBatchAsync(IEnumerable <LogEvent> events)
        {
            if (events == null)
            {
                throw new ArgumentNullException(nameof(events));
            }
            var logGroupInfo = new LogGroupInfo
            {
                Source = source,
                Topic  = topic,
            };

            if (logTags != null)
            {
                logGroupInfo.LogTags = logTags;
            }
            foreach (var logEvent in events)
            {
                var contents = new Dictionary <string, string>(logEvent.Properties.ToDictionary(k => k.Key, v => v.Value.ToString()))
                {
                    { "Level", logEvent.Level.ToString() }
                };

                if (logEvent.Exception != null)
                {
                    contents.Add("Exception", logEvent.Exception.ToString());
                }

                var stringBuilder = new StringBuilder();
                using (var stringWriter = new StringWriter(stringBuilder))
                {
                    this.formatter.Format(logEvent, stringWriter);
                }
                contents.Add("Message", stringBuilder.ToString());

                logGroupInfo.Logs.Add(new LogInfo
                {
                    Time     = logEvent.Timestamp,
                    Contents = contents
                });
            }
            try
            {
                var response = await logServiceClient.PostLogStoreLogsAsync(this.logstoreName, logGroupInfo, hashKey : Guid.NewGuid().ToString("N"), project : project);

                if (!response.IsSuccess)
                {
                    //REDO
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Exemplo n.º 7
0
        public static async Task PostLogStoreLogs(ILogServiceClient client)
        {
            // 原始日志
            var rawLogs = new[]
            {
                "2018-05-04 12:34:56 INFO id=1 status=foo",
                "2018-05-04 12:34:57 INFO id=2 status=bar",
                "2018-05-04 12:34:58 INFO id=1 status=foo",
                "2018-05-04 12:34:59 WARN id=1 status=foo",
            };

            // 解释 LogInfo
            var parsedLogs = rawLogs
                             .Select(x =>
            {
                var components = x.Split(' ');

                var date   = components[0];
                var time   = components[1];
                var level  = components[2];
                var id     = components[3].Split('=');
                var status = components[4].Split('=');

                var logInfo = new LogInfo
                {
                    Contents =
                    {
                        { "level",   level     },
                        { id[0],     id[1]     },
                        { status[0], status[1] },
                    },
                    Time = DateTimeOffset.ParseExact($"{date} {time}", "yyyy-MM-dd HH:mm:ss", null)
                };

                return(logInfo);
            })
                             .ToList();

            var logGroupInfo = new LogGroupInfo
            {
                Topic   = "example",
                Source  = "test",
                LogTags =
                {
                    { "example", "true" },
                },
                Logs = parsedLogs
            };

            var response = await client.PostLogStoreLogsAsync(LogStoreName, logGroupInfo);

            // 此接口没有返回结果,确保返回结果成功即可。
            response.EnsureSuccess();
        }
Exemplo n.º 8
0
        /// <summary>
        ///     日志发送基础方法
        /// </summary>
        /// <param name="keyValuePairs">键值对</param>
        public async void BaseLogSend(Dictionary <string, string> keyValuePairs)
        {
            try
            {
                if (keyValuePairs == null || !keyValuePairs.Any())
                {
                    return;
                }
                if (!options.Enable)
                {
                    return;
                }
                if (Client == null)
                {
                    return;
                }
                var list    = new List <LogInfo>();
                var logItem = new LogInfo
                {
                    Time = DateTimeOffset.Now
                };
                foreach (var keyValuePair in keyValuePairs)
                {
                    if (!string.IsNullOrEmpty(keyValuePair.Key) && !string.IsNullOrEmpty(keyValuePair.Value))
                    {
                        logItem.Contents.Add(keyValuePair.Key, keyValuePair.Value);
                    }
                }
                list.Add(logItem);

                var logGroupInfo = new LogGroupInfo
                {
                    Topic  = name ?? "",
                    Logs   = list,
                    Source =
                        $"[{(options.IsDevelopmentModel ? options.DevelopmentSourcePrefix : options.OnlineSourcePrefix)}]{options.SourceName}"
                };

                await Client.PostLogStoreLogsAsync(options.LogStoreName, logGroupInfo);
            }
            catch (Exception)
            {
                // 作为日志模块不抛出异常
            }
        }
Exemplo n.º 9
0
        public static void DoPutAsync()
        {
            if (logs.Count == 0)
            {
                return;
            }

            //这里需要是异步
            LogInfo[] arr = null;
            lock (logs)
            {
                arr = logs.ToArray();
                logs.Clear();
            }
            var logGroupInfo = new LogGroupInfo
            {
                Topic   = _appName,
                Logs    = arr,
                Source  = System.Environment.MachineName,
                LogTags =
                {
                    { "ver", _ver },
                },
            };

            try
            {
                var response = _client.PostLogStoreLogsAsync(_config.LogstoreName, logGroupInfo).Result;
                // 此接口没有返回结果,确保返回结果成功即可。
                if (!response.IsSuccess)
                {
                    System.Diagnostics.Debug.WriteLine($"上传日志错误,RequestId:{  response.RequestId},code:{response.Error.ErrorCode} msg:{response.Error.ErrorMessage}");
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"上传日志错误,RequestId:{ex}");
            }
        }
Exemplo n.º 10
0
 public static LogGroup ToProtoModel(this LogGroupInfo domain)
 => domain == null
         ? null
         : new LogGroup
 {
     Topic   = domain.Topic,
     Source  = domain.Source,
     LogTags =
     {
         domain.LogTags?
         .Select(x => new LogTag
         {
             Key   = x.Key,
             Value = x.Value ?? String.Empty             // Empty is allowed, but not null.
         })
         ?? Enumerable.Empty <LogTag>()
     },
     Logs =
     {
         domain.Logs?
         .Select(x => x.ToProtoModel())
         ?? Enumerable.Empty <Log>()
     }
 };
Exemplo n.º 11
0
        public async Task DoAsync()
        {
            var client = LogServiceClientBuilders.HttpBuilder
                         .Endpoint("cn-shanghai.log.aliyuncs.com", "wgshop")
                         .Credential("LTAI4Fdcw8xRXhJieZQ1VyYY", "C0ICoLskAsoaRBzThQeHBGI3LMRbs0")
                         .Build();

            // 原始日志
            var rawLogs = new[]
            {
                "2019-11-12 12:34:56 INFO id=1 status=foo",
                "2019-11-12 12:34:57 INFO id=2 status=bar",
                "2019-11-12 12:34:58 INFO id=1 status=foo",
                "2019-11-12 12:34:59 WARN id=1 status=foo",
            };

            // 解释 LogInfo
            var parsedLogs = rawLogs
                             .Select(x =>
            {
                var components = x.Split(' ');

                var date   = components[0];
                var time   = components[1];
                var level  = components[2];
                var id     = components[3].Split('=');
                var status = components[4].Split('=');

                var logInfo = new LogInfo
                {
                    Contents =
                    {
                        { "level",   level     },
                        { id[0],     id[1]     },
                        { status[0], status[1] },
                    },
                    Time = DateTimeOffset.ParseExact($"{date} {time}", "yyyy-MM-dd HH:mm:ss", null)
                };

                return(logInfo);
            })
                             .ToList();

            var logGroupInfo = new LogGroupInfo
            {
                Topic   = "example",
                LogTags =
                {
                    { "example", "true" },
                },
                Source = "test",
                Logs   = parsedLogs
            };

            var response = await client.PostLogStoreLogsAsync("wgshop1", logGroupInfo);

            //var response = await client.PostLogStoreLogsAsync(new PostLogsRequest(),)

            // 此接口没有返回结果,确保返回结果成功即可。
            var result = response.EnsureSuccess();
            var abc    = 111;
        }
Exemplo n.º 12
0
        public IActionResult Post()
        {
            // 1. 构建 `ILogServiceClient` 实例:
            var client = LogServiceClientFactory.BuildSimpleClient();

            // 2.模拟Log日志
            // 注意!!此处时间为LogHub查询时对应的时间
            var rawLogs = new[]
            {
                "2020-02-17 12:34:56 INFO id=1 status=foo",
                "2020-02-18 14:00:00 INFO id=2 status=bar",
                "2020-02-16 12:34:58 INFO id=1 status=foo",
                "2020-02-17 23:59:00 WARN id=1 status=foo",
                "2020-02-04 23:59:00 WARN id=1 status=foo"
            };

            // 3.将原始日志转换为 LogInfo
            var parsedLogs = rawLogs
                             .Select(x =>
            {
                var components = x.Split(' ');

                var date   = components[0];
                var time   = components[1];
                var level  = components[2];
                var id     = components[3].Split('=');
                var status = components[4].Split('=');

                var logInfo = new LogInfo
                {
                    Contents =
                    {
                        { "level",   level     },
                        { id[0],     id[1]     },
                        { status[0], status[1] },
                    },
                    Time = DateTimeOffset.ParseExact($"{date} {time}", "yyyy-MM-dd HH:mm:ss", null)
                };

                return(logInfo);
            })
                             .ToList();
            var logGroupInfo = new LogGroupInfo
            {
                Topic   = "topic_test",
                LogTags =
                {
                    { "example", "true" },
                },
                Logs   = parsedLogs,
                Source = "dq"
            };

            // 4.将日志发送给对应的日志库
            string logstore = "net-logstore";
            var    response = client.PostLogStoreLogsAsync(new PostLogsRequest(logstore, logGroupInfo));


            // 该方法无返回值必要,success仅为方便测试
            return(Ok("success"));
        }