Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logs not writing to ElasticSearch from Xamarin Applications #134

Closed
1 of 7 tasks
alexmartinezm opened this issue Dec 18, 2017 · 7 comments
Closed
1 of 7 tasks

Logs not writing to ElasticSearch from Xamarin Applications #134

alexmartinezm opened this issue Dec 18, 2017 · 7 comments

Comments

@alexmartinezm
Copy link

alexmartinezm commented Dec 18, 2017

Does this issue relate to a new feature or an existing bug?

  • Bug
  • New Feature

What version of Serilog.Sinks.Elasticsearch is affected? Please list the related NuGet package.
v5.5.0

What is the target framework and operating system? See target frameworks & net standard matrix.

  • netCore 1.0
  • netCore 2.0
  • 4.5.x
  • 4.6.x
  • 4.7

This happens in Specific Platfom: Xamarin.Android and Xamarin.iOS.

Please describe the current behavior?
When I set the configuration from a Console Application project, this is the following code I use:

var logger = new LoggerConfiguration()
                .MinimumLevel.Is(level)
                .WriteTo.AndroidLog()
                .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(GlobalUrLs.ElasticUrl))
                {
                    IndexFormat = "mobile"
                })
                .CreateLogger();

Using the same NuGet versions, I use the same code for my Xamarin Application, but it does not send any log to my Elastic's Server.

Please describe the expected behavior?
It should send the logs to Elastic, as it is working with the Console Application.

If the current behavior is a bug, please provide the steps to reproduce the issue and if possible a minimal demo of the problem
Inside a Cross-Platform project, copy the code below (where GlobalUrLs.ElasticUrl is a valid IP, such 192.168.1.20:9200):

var logger = new LoggerConfiguration()
 .MinimumLevel.Is(level)
 .WriteTo.AndroidLog()
.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(GlobalUrLs.ElasticUrl))
{
 IndexFormat = "mobile"
})
.CreateLogger();

Log some message using Serilog:
Log.Information("Testing Serilog from Xamarin Application.");

It should post the log to the server, but it does not.

@alexmartinezm alexmartinezm changed the title Not working when sending logs from Xamarin Logs not writing to ElasticSearch from Xamarin Applications Dec 18, 2017
@mivano
Copy link
Contributor

mivano commented Dec 18, 2017

Unfortunately, I m not experienced with Xamarin. Did you try enabling the selflog (https://github.com/serilog/serilog/wiki/Debugging-and-Diagnostics) capabilities of Serilog and see if it outputs some useful information there?

@alexmartinezm
Copy link
Author

Hello @mivano,

I have enabled SelfLog and it was reporting a not implemented exception in System.Net.Http.HttpClientHandler.set_MaxConnectionsPerServer. After searching in "Issues" inside this repo, I found the solution setting ConnectionLimit to -1. Here is the updated function:

SelfLog.Enable(msg => Android.Util.Log.Debug("SelfLog Serilog", msg));

var logger = new LoggerConfiguration()
                .MinimumLevel.Is((LogEventLevel)level)
                .WriteTo.AndroidLog()
                .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(GlobalUrLs.ElasticUrl))
                {
                    ModifyConnectionSettings = config => config.ConnectionLimit(-1),
                    IndexFormat = "mobile"
                })
                .CreateLogger();

SelfLog.Enable(msg => Android.Util.Log.Debug("SelfLog Serilog", msg));

Even with these changes, my Xamarin Application is still unable to send Logs to my Elastic Server.

@alexmartinezm
Copy link
Author

After the last update on my function, suddently a bulk was sent to my Elastic Server!
Thank you, @mivano.

@mivano
Copy link
Contributor

mivano commented Dec 20, 2017

Good to see it is solved. Make sure to flush when your application closes as it might still have items in the buffer and is unable to deliver when quitting too early.

@Cheesebaron
Copy link

Cheesebaron commented Sep 21, 2018

This is still an issue.

It seems like the managed HttpClientHandler mono provides does not support setting MaxConnectionsPerServer and throws.

Somewhere inside of Elasticsearch.NET they write:

var handler = new HttpClientHandler()

This means, even switching to AndroidHttpClientHandler or NSUrlSessionHandler in project properties does not kick in.

A way to get around this is to provide your own HttpConnection in ModifyConnectionSettings in ElasticSearchOptions:

Log.Logger = configuration
    .WriteTo.Elasticsearch(
        new ElasticsearchSinkOptions(esUrl)
        {
            ModifyConnectionSettings = c =>
            {
                var connection = new MyHttpConnection(handler);
                var pool = new SingleNodeConnectionPool(esUrl);
                return new ConnectionConfiguration(pool, connection);
            },

Where MyHttpConnection overrides CreateHttpClientHandler()

@mivano
Copy link
Contributor

mivano commented Sep 21, 2018

thanks for the feedback. Maybe something to raise on the Elasticsearch repo?

@Cheesebaron
Copy link

@mivano I've done that here: elastic/elasticsearch-net#3415

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants