maxJsonLength 超出了 log4net ElasticSearch appender 的日志记录

maxJsonLength exceeded logging with log4net ElasticSearch appender

我们正在使用 log4net.ElasticSearch appender,将应用程序日志消息发送到 ElasticSearch。

出于 debugging/support 目的,我们经常将服务请求和响应记录为上下文对象以记录事件 - 似乎有时序列化时的响应可能太大,这会导致以下异常:

System.InvalidOperationException occurred
  HResult=-2146233079
  Message=Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.
  Source=System.Web.Extensions
  StackTrace:
   at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat)
   at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, SerializationFormat serializationFormat)
   at log4net.ElasticSearch.ExtensionMethods.ToJson[T](T self)
  InnerException: 

有什么方法可以增加这个限制吗?

您可以为 appender 发出拉取请求:

https://github.com/jptoto/log4net.ElasticSearch/blob/f5d122526f410d1294bc8d3aeb5a94379800f552/src/log4net.ElasticSearch/ExtensionMethods.cs

    public static string ToJson<T>(this T self)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        serializer.MaxJsonLength = Int32.MaxValue;
        return serializer.Serialize(self);
    }