locked
not able to write log messages in event log on windows 2008 server

    Question

  • Hi,

    I am trying to write in Event Log on windows 2008 server with a windows service application. But I got this error:

    Service cannot be started. System.ComponentModel.Win32Exception: The parameter is incorrect at System.Diagnostics.EventLog.InternalWriteEvent(UInt32 eventID, UInt16 category, EventLogEntryType type, String[] strings, Byte[] rawData, String currentMachineName) at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData) at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type) at Siemens.SHS.Utilities.Common.LogManager.LogToEvent(String pLogName, String pSource, String logEntry, EventLogEntryType type) at Siemens.SHS.PEP.TaskCore.StartTask(String pPluginFilePath) at Siemens.SHS.PEP.TaskCore.RecurseStart(String pPluginPath) at Siemens.SHS.PEP.TaskCore.RecurseStart(String pPluginPath) at Siemens.SHS.PEP.TaskCore.StartTasks() at Siemens.SHS.PEP.PlugnExecute.OnStart(String[] args) at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)

    How can I write to Event Log on x64 platform as Windows 2008 server?

    Any help appreciated, thanks in advance,

    Cigdem

    Tuesday, January 26, 2010 1:29 PM

All replies

  • Hi,
    Could you post some code snippet that bring you the exception?
    Sincerely,
    Eric

    Please remember to mark helpful replies as answers.
    Thursday, January 28, 2010 8:43 AM
  • Hi Lads!

    I had the same problem, but I was lucky enough to get another error just before it that gave me a good hint on where to look:

    An Exception error occured during CRM call : System.ArgumentException: Log entry string is too long. A string written to the event log cannot exceed 32766 characters.  at System.Diagnostics.EventLog.InternalWriteEvent(UInt32 eventID, UInt16 category, EventLogEntryType type, String[] strings, Byte[] rawData, String currentMachineName)    at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData).........

    Afterwards I added a check that just cut off the string at 32765 chars. This though DID NOT solve it, it just gave me another exception! The same code turned out to this error message:

    System.ComponentModel.Win32Exception: The parameter is incorrect     at System.Diagnostics.EventLog.InternalWriteEvent(UInt32 eventID, UInt16 category, EventLogEntryType type, String[] strings, Byte[] rawData, String currentMachineName)   at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData)

    I then cut it at 30000 chars instead, and that resolved my problem.

    I suspect that they do a check if your text is too long according to their max length (32766) and throw a nice exception there, but actually their code still crashes at less chars and that it just throws this other "not as nice" exception at a later point instead. Just an idea.. I don't actually know this for sure.

    Wednesday, September 08, 2010 8:55 AM
  • I know this is a very old post but I just ran across the same issue and thought that you might like to know that the "magic number" of characters is 31878. Anything more than that throws an exception.
    Tuesday, June 07, 2011 4:20 PM
  • Steven, based on my calculations 31894 is the value I came up with for Windows 7 and 31895 for Windows Server 2008.

    Here's the magic formula:

    String length of the Source parameter + String length of the Message parameter <= 31894

    For those interested, here's the source code for a test console application:

            static void Main(string[] args)
            {
                string strNumCharacters, message = string.Empty;

                //Prompt user for characters
                Console.WriteLine("Enter a number of characters to generate. [On Windows 7 Pro and Win2k8: for Success: 31889. For Failure: 31890 (on Win7) and 31891 (on Win2k8) ]");
                strNumCharacters = Console.ReadLine();
               
                //Convert string to int
                int numCharacters = int.Parse(strNumCharacters);
               
                //Generate characters
                for (int i = 0; i < numCharacters; i++)
                {
                    message += "0";
                }

                //Write to characters to EventViewer
                string logName = "Application";
                string source = "MyApp"; //note MyApp is 5 characters long so 31894 - 5 = 31889 (max string length)
                int eventId = 1;

                if (!EventLog.SourceExists(source))
                    EventLog.CreateEventSource(source, logName);

                try
                {
                    EventLog.WriteEntry(source, message, EventLogEntryType.Information, eventId);
                    Console.WriteLine("Success");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

                Console.ReadLine();
            }

     

    HTH

    Bill




    Monday, August 22, 2011 8:00 PM