1

I am trying to log into Elasticsearch in one of my ASP.NET Web API project using Serilog, but unfortunately, I can't find the logs in Kibana.

public class Logger
{
    private readonly ILogger _localLogger;

    public Logger()
    {
        ElasticsearchSinkOptions options = new ElasticsearchSinkOptions(new Uri("xxx"))
        {
            IndexFormat = "log-myservice-dev",
            AutoRegisterTemplate = true,
            ModifyConnectionSettings = (c) => c.BasicAuthentication("yyy", "zzz"),
            NumberOfShards = 2,
            NumberOfReplicas = 0
        };

        _localLogger = new LoggerConfiguration()
                            .MinimumLevel.Information()
                            .WriteTo.File(HttpContext.Current.Server.MapPath("~/logs/log-.txt"), rollingInterval: RollingInterval.Day)
                            .WriteTo.Elasticsearch(options)
                            .CreateLogger();
    }

    public void LogError(string error)
    {
        _localLogger.Error(error);
    }

    public void LogInformation(string information)
    {
        _localLogger.Information(information);
    }
}

I can see the logs in the file specified above, just not in Elasticsearch. So, I am wondering is there is any way I can debug why it failed to log into Elasticsearch? I am also open to using other logging framework to log into Elasticsearch.

*The credentials and url for Elasticsearch are valid as I have implemented this in my other AWS Lambda project (.net core).

4
  • Have you tried to look for error -> github.com/serilog-contrib/… I believe you can log such error locally to debug Commented Jan 5, 2022 at 13:11
  • Yeap, that did the trick. Commented Jan 5, 2022 at 15:38
  • so what is the issue then XD or did you already fix it ? Commented Jan 5, 2022 at 15:45
  • The issue was with not being able to establish a HTTPS connection to elasticsearch. Added the error message + solution. Commented Jan 5, 2022 at 15:58

1 Answer 1

5

To see exactly what went wrong, the easiest way is to write into console, and in case of ASP.NET project, it will be Debug.WriteLine. So the code to see what went wrong would be

Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));

ElasticsearchSinkOptions options = new ElasticsearchSinkOptions(new Uri("xxx"))
{
         IndexFormat = "log-myservice-dev",
         AutoRegisterTemplate = true,
         ModifyConnectionSettings = (c) => c.BasicAuthentication("yyy", "zzz"),
         NumberOfShards = 2,
         NumberOfReplicas = 1,
         EmitEventFailure = EmitEventFailureHandling.WriteToSelfLog,
         MinimumLogEventLevel = Serilog.Events.LogEventLevel.Information
};

The following error message was retrieved from the output console.

Failed to create the template. Elasticsearch.Net.ElasticsearchClientException: The request was aborted: Could not create SSL/TLS secure channel.. Call: Status code unknown from: HEAD /_template/serilog-events-template ---> System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel.

The issue is quite clear cut. Added the following in my logger class constructor helped with the issue.

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

Hope it helps others that encounter issue trying to use Serilog to log into Elasticsearch for .Net Framework.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.