c# - 如何在 ASP.Net Web API 中摄取大量日志

标签 c# .net rest asp.net-web-api asp.net-web-api2

我是 API 开发的新手,我想创建一个将接收大量日志数据的 Web API 端点。我想通过 Amazon Kinesis 传输流将该数据发送到 Amazon s3 存储桶。下面是一个运行良好的示例应用程序,但我不知道如何摄取大量入站数据以及我的 API 应该以什么格式接收数据?我的 API 端点应该是什么样子。

 [HttpPost]
 public async void Post() // HOW to allow it to receive large chunk of data?
 {
        await WriteToStream();
 }

    private async Task WriteToStream()
    {
        const string myStreamName = "test";
        Console.Error.WriteLine("Putting records in stream : " + myStreamName);
        // Write 10 UTF-8 encoded records to the stream.
        for (int j = 0; j < 10000; ++j)
        {
        // I AM HARDCODING DATA HERE FROM THE LOOP COUNTER!!! 
            byte[] dataAsBytes = Encoding.UTF8.GetBytes("testdata-" + j);
            using (MemoryStream memoryStream = new MemoryStream(dataAsBytes))
            {
                    PutRecordRequest putRecord = new PutRecordRequest();
                    putRecord.DeliveryStreamName = myStreamName;
                    Record record = new Record();
                    record.Data = memoryStream;
                    putRecord.Record = record;
                    await kinesisClient.PutRecordAsync(putRecord);
            }
        }
    }

P.S:在现实世界的应用程序中,我不会有那个 for 循环。我希望我的 API 摄取大数据,我的 API 的定义应该是什么?我是否需要使用名为multiform/datafile 的东西?请指导我。

最佳答案

这是我的思考过程。当您公开用于日志记录的 API 时,您的输入应包含以下属性

  • 日志级别(信息、调试、警告、致命)
  • 日志消息(字符串)
  • 申请编号
  • 应用程序实例 ID
  • 应用IP
  • 主机(记录错误的机器)
  • 用户 ID(发生错误的用户)
  • Utc 时间戳(错误发生的时间)
  • 附加数据(可自定义为 xml/json)

我建议通过 Gateway API 将 API 公开为 AWS lambda,因为它有助于在负载增加时进行扩展。

要了解如何构建 API 和使用模型绑定(bind),您可以引用 https://learn.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/model-validation-in-aspnet-web-api

关于c# - 如何在 ASP.Net Web API 中摄取大量日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54642926/

相关文章:

c# - 在 Visual C# 中获取 CPU 多核使用情况

c# - 使用紧凑框架 3.5 从数据网格中检索数据

c# - 将对象转换为 XML 字符串

c# - .Net Rest Web 服务响应具有默认的数据协定命名空间而不是预期的命名空间

web-services - SOAP 和 REST Web 服务在 Java EE 中具有一种实现

c# - 如何获取 Datagridview Combobox 所选项目的文本?

c# - 使用外部 SAML 身份提供商登录 SimpleMembership 应用程序

c# - 如何读取 FOR XML SQL Server 查询的完整结果?

java - JMX 和 REST API。我怎样才能在他们之间架起一座桥梁?

rest - 在 REST API 中,DELETE 方法可以有参数吗?