Message-Längen Beschränkung im Event Log

Wenn eine lange Message in das Windows EventLog mit System.Diagnostics.EventLog.WriteEntry geschrieben wird…

An unhandled exception of type ‚System.ArgumentException‘ occurred in System.dll

Additional information: Log entry string is too long. A string written to the event log cannot exceed 32766 characters.

Klar genug, so scheint es. Jedoch Überraschung; beschränkt man die Message auf die mitgeteilte Länge von 32766 (oder ein paar weniger) Zeichen, entsteht wieder eine Exception, dieses Mal nicht so aussagekräftig:

An unhandled exception of type ‚System.ComponentModel.Win32Exception‘ occurred in System.dll

Additional information: The parameter is incorrect

Die aktuelle Dokumentation

            https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.eventlog
bzw. die Dokumentation der WinAPI, auf der das beruht
            https://docs.microsoft.com/de-de/windows/desktop/api/winbase/nf-winbase-reporteventa
macht dann auch klar, dass die eigentliche Maximal-Länge seit Windows Vista bei
            31839 Zeichen 
liegt. Der Argument-checking Code der EventLog.WriteEntry Methode ist nur offenbar noch nicht angepasst.

Das Ganze zusammengefasst in einem Code-Snippet.

// An unhandled exception of type ‚System.ArgumentException‘ occurred in System.dll

// Additional information: Log entry string is too long.

// A string written to the event log cannot exceed 32766 characters.

//Int32 maxLength = 33000;

 

// An unhandled exception of type ‚System.ComponentModel.Win32Exception‘ occurred in System.dll

// Additional information: The parameter is incorrect

//Int32 maxLength = 32766;

 

// Success!

Int32 maxLength = 31839;

 

String msgShortened = msg.Length > maxLength ? msg.Substring(0, maxLength) : msg;

EventLog.WriteEntry(„Xperiment“, msgShortened, EventLogEntryType.Information, 55555);

Viel Spass beim Coding!

Plamen Petrow
QUIBIQ Schweiz