Facing issue to handle to log only the loggs when the cluster is down.
I did try with the below implemenation but when the elasticsearch culster is down the loggs are not being logged to the secondary sink.
var apiKeyElasticSearch = "<strong>";
var elasticUrl = "http://</strong> *:9200";
First Example:
Log.Logger = new LoggerConfiguration()
.WriteTo.FallbackChain(
wt => wt.Elasticsearch(
new[] { new Uri(elasticUrl) },
options =>
{
options.DataStream = new DataStreamName("errors", "ms", "dev");
options.IlmPolicy = "errors-retention-policy";
options.TextFormatting = new EcsTextFormatterConfiguration<LogEventEcsDocument>();
options.BootstrapMethod = BootstrapMethod.Failure;
options.ConfigureChannel = channelOptions =>
{
channelOptions.BufferOptions = new BufferOptions
{
ExportMaxConcurrency = 10,
ExportMaxRetries = 0
};
};
},
transport =>
{
transport.Authentication(new ApiKey(apiKeyElasticSearch));
}
),
// Fallback: Windows Event Log
wt => wt.EventLog("Sample App", manageEventSource: true)
)
.CreateLogger();
Second Example
public class MyFailureListener : ILoggingFailureListener
{
public void OnLoggingFailed(object sender, LoggingFailureKind kind, string message, IReadOnlyCollection<LogEvent>? events, Exception? exception)
{
using (var eventLog = new EventLog("Application") { Source = ".NET Runtime" })
{
eventLog.WriteEntry("Testing success", EventLogEntryType.Error, 1000);
}
}
}
var listener = new MyFailureListener();
Log.Logger = new LoggerConfiguration()
.WriteTo.Fallible(
wt => wt.Elasticsearch(new[] { new Uri(elasticUrl) },
options =>
{
options.DataStream = new DataStreamName("errors", "ms", "dev");
options.IlmPolicy = "errors-retention-policy";
options.TextFormatting = new EcsTextFormatterConfiguration<LogEventEcsDocument>();
options.BootstrapMethod = BootstrapMethod.Failure;
options.ConfigureChannel = channelOptions =>
{
channelOptions.BufferOptions = new BufferOptions
{
ExportMaxConcurrency = 10,
ExportMaxRetries = 0
};
};
},
transport =>
{
transport.Authentication(new ApiKey(apiKeyElasticSearch));
//transport.ServerCertificateValidationCallback((_, _, _, _) => true);
}),
listener
)
.CreateLogger();