Tuesday 31 August 2021

Using AWS Elastic Search with Serilog in .Net and inspecting the actual Message

 AWS provides a managed service for Elastic Search + Kibana for everyone who wants (and can pay 😀)



Bellow, I will provide an example about how to write a log to Elastic and also to log the actual HTTP request/response from Elastic.

For the logging, I will use the very famous Serilog library that allows logging the message to different outputs (Console/File/ElasticSearch....) very easily.

Create a new console project and add the following Nuget packages.



The logon purposes you need to have an AWS account and to know all the credentials. See code inline comments to understand the purposes

class Program

    {
       
        //capture the response/request to the console
        public static void RequestCompleted(IApiCallDetails le)
        {
            Console.WriteLine(le.DebugInformation);
        }

        static void Main(string[] args)
        {
            //Logging Credentials/Data
            Environment.SetEnvironmentVariable("AWS_ACCESS_KEY_ID", "XXXXXXXXX");
            Environment.SetEnvironmentVariable("AWS_SECRET_ACCESS_KEY", "XXXXXXXXXXXXXXXXX");
            Environment.SetEnvironmentVariable("AWS_REGION", "eu-central-1");

            //Tell Serilog to write messages to Elastic
            Log.Logger = new LoggerConfiguration()
            .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("https://somehost_in_aws"))
            {
                BatchPostingLimit = 1,
               
                ModifyConnectionSettings = conn =>
                {
                    var httpConnection = new AwsHttpConnection("eu-central-1");
                    var pool = new SingleNodeConnectionPool(new Uri("https://somehost_in_aws"));
                    var conf = new ConnectionConfiguration(pool, httpConnection);
                    
                    //Show the message pritty printed
                    conf.EnableDebugMode();
                    conf.PrettyJson();
                    //this is the callback that will handle printing of the request/response
                    conf.OnRequestCompleted(e=> RequestCompleted(e));

                    return conf;
                },
                //the name of the index (some kind of the table) that will be created in Elastc
                IndexFormat = "itest-{0:yyyy.MM}",
            })
            //We also want to see the messages in the console
            .WriteTo.Console()
            .CreateLogger();

            //this message will be sent to the console and also to Elastic
            Log.Logger.Error("Error Message from Demo");
           
            Console.ReadKey();

        }
    }

After we run the program in the console we will see the actual request/response





No comments:

Post a Comment