Details
Description
Issue: EventLogAppender can corrupt the event log on Windows Vista and higher if the string is longer than 31839 bytes.
The log4net EventLogAppender allows you to write up to 32000 bytes and there is a defect in the .NET Framework that will let you write that much data in Windows Vista and higher (which has a limit of 31839).
See the attachment that shows "$exception
{"The event log file is corrupted"}System.Exception
{System.ComponentModel.Win32Exception}" along with the stack after a call to EventLog.WriteEntry() under Windows 7 that used a message string that was 31,876 bytes long.
The issue has been reported to Microsoft as well: https://connect.microsoft.com/VisualStudio/feedback/details/770126/eventlog-writeentry-can-corrupt-the-event-log-because-of-invalid-argument-check-in-net-framework#tabs
The code below is my workaround for determining a max message length that will not corrupt the event log. I've also written our own custom EventLogAppender that is able to work around this issue.
private const int MaxEventLogMsgLength_PreVista = 32766;
private const int MaxEventLogMsgLength_VistaOrHigher = 31839;
/// <summary>
/// Gets the maximum allowable size of event log message for the current operating system.
/// </summary>
/// <returns></returns>
public static int GetMaxEventLogMessageSize()
{
// http://msdn.microsoft.com/en-us/library/xzwc042w(v=vs.100).aspx
// The 32766 documented max size is two bytes shy of 32K (I'm assuming 32766 may leave space for a two byte null
// terminator of #0#0). The 32766 max length is what the .NET 4.0 source code checks for, but this is WRONG!...
// strings with a length > 31839 on Windows Vista or higher can CORRUPT the event log! See:
// System.Diagnostics.EventLogInternal.InternalWriteEvent() for the use of the 32766 max size.
var maxEventMsgSize = MaxEventLogMsgLength_PreVista;
// Windows Vista and higher
if (Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version.Major >= 6)
return maxEventMsgSize;
}
Attachments
Attachments
Issue Links
- breaks
-
LOG4NET-382 TargetInvocationException occurs because MESSAGE_SIZE fields in EventLogAppender are initialized in wrong order
- Closed