我正在嘗試使用 Serilog 在我的 ASP.NET Web API 專案之一中登錄 Elasticsearch,但不幸的是,我在 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);
}
}
我可以在上面指定的檔案中看到日志,只是在 Elasticsearch 中看不到。所以,我想知道有什么方法可以除錯為什么它無法登錄到 Elasticsearch?我也愿意使用其他日志框架來登錄 Elasticsearch。
*Elasticsearch 的憑證和 url 是有效的,因為我已經在我的其他 AWS Lambda 專案(.net 核心)中實作了這一點。
uj5u.com熱心網友回復:
要查看到底出了什么問題,最簡單的方法是寫入控制臺,對于 ASP.NET 專案,它將是 Debug.WriteLine。所以看看出了什么問題的代碼是
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
};
從輸出控制臺檢索到以下錯誤訊息。
創建模板失敗。Elasticsearch.Net.ElasticsearchClientException:請求被中止:無法創建 SSL/TLS 安全通道。呼叫:狀態代碼未知來自:HEAD /_template/serilog-events-template ---> System.Net.WebException:請求是中止:無法創建 SSL/TLS 安全通道。
這個問題非常明確。在我的記錄器類建構式中添加以下內容有助于解決該問題。
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
希望它可以幫助其他人在嘗試使用 Serilog 登錄 Elasticsearch for .Net Framework 時遇到問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/408453.html
標籤:
