1

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?

azzurro123
  • 521
  • 2
  • 10
  • 19
  • 1
    Did you try to remove this : `.WriteTo.File($"./failures.log", rollingInterval: RollingInterval.Day)`. You cannot write to a file in Blazor WASM. Actually I'll removed all but Elasticsearh config. – agua from mars Aug 13 '20 at 09:01
  • 1
    Try to enable Serilog diagnostics too. https://github.com/serilog/serilog/wiki/Debugging-and-Diagnostics – agua from mars Aug 13 '20 at 09:04
  • ah indeed, thanks - no writing to a file - static web application - I turned on Serilog diagnostics just now: – azzurro123 Aug 13 '20 at 09:15
  • Caught exception while preforming bulk operation to Elasticsearch: Elasticsearch.Net.UnexpectedElasticsearchClientException: Property AutomaticDecompression is not supported. ---> System.PlatformNotSupportedException: Property AutomaticDecompression is not supported. – azzurro123 Aug 13 '20 at 09:16
  • 1
    So it means it's not supported by blazor WASM, May be you can write your own Elasticseach sync or fix the sync for Blazor WASM or configure the sync to not use AutomaticDecompression if it works – agua from mars Aug 13 '20 at 09:19
  • OMG - I made it work - Blazor WASm partially supports the HttpClientHandler, so I made a custom derived implementation of HttpClientHandler where I set AutomaticDecompression to false and then create my own HttpConnection implementation and override the Connection setting in ElasticsearchSinkOptions... – azzurro123 Aug 13 '20 at 14:25
  • @aguafrommars thanks a lot, how can I pay you a beer :D – azzurro123 Aug 13 '20 at 14:26
  • 4
    Write an answer, it'll help other – agua from mars Aug 13 '20 at 15:35
  • My working solution logs to Elasticsearch but still runs into an error: Cannot wait on monitors. I posted my answer in a new question: https://stackoverflow.com/questions/63408984/configure-serilog-elasticsearch-in-blazor-webassembly – azzurro123 Aug 14 '20 at 08:13

0 Answers0