微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

非静态字段、方法或属性“Program.InsertLogData()”需要对象引用

如何解决非静态字段、方法或属性“Program.InsertLogData()”需要对象引用

执行建议的代码后,我收到一条错误消息:

An Object reference is required for the non-static field,method,or property Program.InsertLogData()

对于 await insertlogdata() 的 try 块

我是否应该删除 main 中的 static 一词并将其保留为公共异步任务?

在给出建议后更新代码

命名空间 ElasticSearchConsoleApp { 课程计划 { 私有只读 IElasticclient _elasticclient;

    public Program()
    {
        _elasticclient = new Elasticclient();
    }
    public static async Task Main(string[] args)
    {
        Console.Write("getting connection...");


        try
        {
            await InsertLogData();
        }
        catch(Exception ex)
        {
            Console.Write("Error: " + ex.Message);
        }

        Console.ReadLine();


    }

    public async Task<int> InsertLogData()
    {
        sqlConnection connection = null;
        sqlCommand command = null;
        int numrows = 0;

        try
        {
            var response = await _elasticclient.SearchAsync<Object>(s => s
                .Size(3000)
                .Index("customer-simulation-es-app-logs*")
                .Query(q => +q
                    .Daterange(dr => dr
                        .Field("@timestamp")
                            .GreaterThanorEquals("2021-06-07T17:13:54.414-05:00")
                            .LessthanorEquals(DateTime.Now))));

            connection = new sqlConnection("Data Source=.\\sqlExpress;Database=ElasticSearchService;Trusted_Connection=True;");

            connection.open();

            foreach (var item in response.Hits)
            {
                var id = item.Id;
                var sourceItem = item.source;

                var json = _elasticclient.RequestResponseSerializer.SerializetoString(sourceItem);

                command = new sqlCommand("INSERT INTO EsLogs (ELKLogID,LogMessage,DateCreated) VALUES ('" + id + "','" + json + "',getdate())",connection);

                numrows = command.ExecuteNonQuery();
            }

            connection.Close();

        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
        }
        finally
        {
            command.dispose();
            connection.dispose();


        }
        return numrows;
    }

}

}

enter image description here

解决方法

你必须修复弹性客户端。

private static readonly IElasticClient _elasticClient = new ElasticClient();

//  public Program()
//  {
//  }

在此之后,您将遇到另一个错误。你必须通过放置来修复你的 foreach 循环 循环内的 command.ExecuteNonQuery


 connection.Open();
foreach (var item in response.Hits)
 {
 var id = item.Id;
  var sourceItem = item.Source;
 var json = _elasticClient.RequestResponseSerializer.SerializeToString(sourceItem);

 command = new SqlCommand("INSERT INTO EsLogs (ELKLogID,LogMessage,DateCreated) VALUES ('" + id + "','" + json + "',getdate())",connection);

 numrows = command.ExecuteNonQuery();
}

 connection.Close();

顺便说一句,您必须考虑为查询使用参数

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?