System.Diagnostics.EventLog.WriteEntry 方法对消息字符串有大小限制。 
如果消息字符串超过 32766 个字节,则会发生异常。
我的程序里偶尔就出现了这个异常 ~ ~请问哪位大哥知道出现这异常了该怎么避免啊?

解决方案 »

  1.   

    这个应该没有办法吧,你只能限制你传入的字符串小于32766 下面来自msdn:http://msdn.microsoft.com/en-us/library/aa363679(VS.85).aspxEach string is limited to 31,839 characters.Prior to Windows Vista:  Each string is limited to 32K characters.
      

  2.   


    我这样限制的,但是报错说out of range!
      if (ErrMessage.Length > 32766)
      {
           ErrMessage = ErrMessage.Substring(0, 3276);
      }
      

  3.   

    findcaiyzh 大哥,继续啊?
    再给指点点啊!
      

  4.   

    ArgumentException 
    该消息字符串的长度超过了 32766 个字节。
    System.Text.Encoding.Default.GetBytes(ErrMessage).Length;
      

  5.   

    ErrMessage.Length+ Regex.Matches(ErrMessage,"[\u0080-\uffff]").Count; 
      

  6.   

    长度单位是字节,不是字符串,所以需要转换成字节数组,然后截取长度。
    System.Text.Encoding.Default.GetBytes(ErrMessage).Length;