的EventLog.WriteEntry方法不接受strigs exceding31839分之32766字節 ...截斷字符串(爲特定字節大小)
我想知道如何將字符串截取到的具體數量字節。
這裏是我的代碼:
public static void Log(string message)
{
const int MaxLogMessageLenght = 32766;
string logMessage = message;
var unicodeByteCount =
System.Text.ASCIIEncoding.Unicode.GetByteCount(logMessage);
var asciiByteCount =
System.Text.ASCIIEncoding.ASCII.GetByteCount(logMessage);
if (unicodeByteCount >= MaxLogMessageLenght)
{
// ????
// Truncate the string to fit the WriteEntry length
// logMessage = message.Substring(0, MaxLogMessageLenght - 5) + "...";
}
EventLog.WriteEntry(LogSource, logMessage, EventLogEntryType.Information);
}
'ASCIIEncoding.Unicode'似乎有點奇怪... o.O –
* Substring *有什麼問題? – EZI
你註釋掉的那一行有什麼問題:'message.Substring(...'? –