我想将日志从 Unity 应用程序发送到 Elasticsearch。然而,Serilog.Sinks.Elasticsearch 在统一内部运行似乎很复杂(如果可能的话)。可能的解决方案是拥有一个 ASP.NET Web API 服务,该服务可以从应用程序检索日志并将它们进一步记录到 Elasticsearch。
我可以在 Unity 应用程序中使用 Serilog.Sinks.Http,在 Web API 中使用 Serilog.Sinks.Elasticsearch。 这是一个好的解决方案吗?
或者可能只是在没有记录器的情况下发送它?从我收到的对象开始。 Serilog.Sinks.Elasticsearch 中是否有特殊功能?
必须有灵活的字段,不需要在服务器端定义DTO。 你能推荐什么?
当我试图解决同样的问题——将日志从 Unity 项目写入远程 Elasticsearch 实例时,我发现了以下异常:
Caught exception while preforming bulk operation to Elasticsearch: Elasticsearch.Net.UnexpectedElasticsearchClientException: The method or operation is not implemented. ---> System.NotImplementedException: The method or operation is not implemented.
at System.Net.Http.MonoWebRequestHandler.set_MaxConnectionsPerServer (System.Int32 value) [0x00000] in <9ba5951f23f145c3931879cd4bb7f4ef>:0
at System.Net.Http.HttpClientHandler.set_MaxConnectionsPerServer (System.Int32 value) [0x00000] in <9ba5951f23f145c3931879cd4bb7f4ef>:0
at Elasticsearch.Net.HttpConnection.CreateHttpClientHandler (Elasticsearch.Net.RequestData requestData) [0x00032] in <0cc67541a10c4390b1929b79f0246c2a>:0
at Elasticsearch.Net.HttpConnection.<.ctor>b__12_0 (Elasticsearch.Net.RequestData r) [0x00000] in <0cc67541a10c4390b1929b79f0246c2a>:0
at Elasticsearch.Net.RequestDataHttpClientFactory.CreateHandlerEntry (System.Int32 key, Elasticsearch.Net.RequestData requestData) [0x00000] in <0cc67541a10c4390b1929b79f0246c2a>:0
at Elasticsearch.Net.RequestDataHttpClientFactory+<>c__DisplayClass15_0.<.ctor>b__1 () [0x00000] in <0cc67541a10c4390b1929b79f0246c2a>:0
at System.Lazy`1[T].ViaFactory (System.Threading.LazyThreadSafetyMode mode) [0x0001c] in <75633565436c42f0a6426b33f0132ade>:0
--- End of stack trace from previous location where exception was thrown ---
(我通过在创建记录器实例之前放置
SelfLog.Enable(Debug.Log);
得到了这个异常。请参阅 documentation 中的详细信息)。
这让我觉得这个接收器可能无法在 Mono 运行时工作。
但后来我发现这个线程。将连接限制设置为
-1
解决了这个问题,日志开始出现在 Elasticsearch 中。但是,我不知道这个解决方案的缺点以及这将如何影响最终用户。特别是,由于 HTTP 连接量较大,我担心移动设备的电池耗尽。该线程中描述了另一种解决方案,但我还没有测试它。
我使用的最终解决方案:
SelfLog.Enable(Debug.Log);
ILogger logger = new LoggerConfiguration()
.WriteTo.Elasticsearch(
new ElasticsearchSinkOptions(new Uri("https://my.domain.com"))
{
ModifyConnectionSettings = settings => settings.ConnectionLimit(-1), // this line fixed the problem
EmitEventFailure = EmitEventFailureHandling.WriteToSelfLog,
})
.CreateLogger();
logger.Information("test");