I'm currently working on a .NET Standard 2.1 Blazor WebAssembly Hosted application.
I use Serilog and I set up ElasticSearch and Kibana on my local machine (localhost:9200).
My project structure looks like this:
- BlazorApp.Client (I want to send logs to ElasticSearch from here)
- BlazorApp.Server
Unfortunatly my BlazorApp.Client cannot connect to ElasticSearch, I cannot use the ElasticSearch middleware in my BlazorApp.Client project: app.UseElasticApm(Configuration);
because it's a static web application.
Though when I send my logs from BlazorApp.Client to an API controller (LogController) running on a server application it works fine - I can show my logs and Kibana finds my Index.
I already followed this post: Serilog Not Logging to Elasticsearch
The difference is, I don't have a Startup class and thus cannot use middleware in a Blazor WASm Hosted Client project!
My BlazorApp.Client Program.cs looks like this:
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");
builder.Services.AddTransient(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
ConfigureLogging(builder);
try
{
Log.Information($"starting test application....");
await builder.Build().RunAsync();
}
catch (Exception e)
{
Log.Error(e.Message);
}
finally
{
Log.Information($"stopping test application.....");
}
}
private static void ConfigureLogging(WebAssemblyHostBuilder builder)
{
builder.Logging.ClearProviders();
Log.Logger = new LoggerConfiguration()
.WriteTo.Debug()
.WriteTo.Console(new CompactJsonFormatter())
.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://localhost:9200"))
{
AutoRegisterTemplate = true,
IndexFormat = $"client-index-{DateTime.UtcNow:yyyy-MM}"
})
.WriteTo.File($"./failures.log", rollingInterval: RollingInterval.Day)
.CreateLogger();
}
}
Do you know if it's possible to use Serilogs .WriteTo.ElasticSearch()
sink in a Blazor WASm client project?
In case, how can I configure it properly that i can find my client-index in Kibana and show the logs accordingly in Kibana?