Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Diagnostics.EventLog - A device attached to the system is not functioning

I did some google and none of the "device attached" errors had anything to do with eventlog. Also I posted the quesiton in Micosoft fourm but had no responses. Thought I would give you guys a shot. Here the link to the question. https://social.msdn.microsoft.com/Forums/en-US/d484d9dc-d9eb-4d19-97b8-9ae4db63e041/systemdiagnosticseventlog-a-device-attached-to-the-system-is-not-functioning?forum=netfxbcl

Here is the error message:

System.ComponentModel.Win32Exception was caught
  ErrorCode=-2147467259
  HResult=-2147467259
  Message=A device attached to the system is not functioning
  NativeErrorCode=31
  Source=System
  StackTrace:
       at System.Diagnostics.EventLogInternal.InternalWriteEvent(UInt32 eventID, UInt16 category, EventLogEntryType type, String[] strings, Byte[] rawData, String currentMachineName)
       at System.Diagnostics.EventLogInternal.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData)
       at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type)
       at VB_Braums_ClassLib.LogIt.WriteEventLog(String Entry, EventLogEntryType eventType, String Source) in \\Corp01\Vol1\Mis\Pccode\Ms.net\ProductionLibs\ProductionLibs\ProdLibCommon.vb:line 3666
  InnerException: 

This code is in a library, so I create a "hello world" to demonstrate the issue. I will post it at the end. This .net 4 framework and it's been around for a while in our code. We are starting to upgrade from XP to Win 7. Basically event log sizes are limited to the 32667 number. So we had a test, if the string is bigger than that, then we would write it in 32000 byte chucks. On two different win7 boxes we get the "device attached" error message. Run the same code on a XP box and it works. Oh, and the Win 7 boxes are 64 bit. I wonder if the win 7 32 bit would have the same issue? Can others duplicate it? The tmpsize seems to be different numbers, but if you play with it, you can get it down to number x works and (x+1) does not. Somewhere between 30000 and 32000. Start with most snighfict value and move your way down, I bet it around 31000 - 31900 range.

Module Module1
    Sub Main()

        Dim logName As String = "BraumsLog"
        Dim objEventLog As New System.Diagnostics.EventLog()
        Dim needCreate As Boolean = False
        Dim Source As String = ""
        If Source.Length = 0 Then Source = "Test"
        Dim Entry As String = "".PadLeft(64000, "1"c)

        'Register the App as an Event Source
        If EventLog.SourceExists(Source) Then
            Dim slog As String = EventLog.LogNameFromSourceName(Source, ".")
            If slog <> logName Then EventLog.DeleteEventSource(Source) : needCreate = True
        Else
            needCreate = True
        End If

        If needCreate Then EventLog.CreateEventSource(Source, logName)
        objEventLog.Source = Source

        '*************************************
        '*********** New Code ****************
        objEventLog.MaximumKilobytes = 20480
        objEventLog.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 0)
        '*************************************
        '*************************************

        'WriteEntry is overloaded; this is one
        'of 10 ways to call it
        Dim tmp As String = ""
        Dim tmpSize As Integer = 32000 '31890 works 31891 does not
        Do While Entry.Length > tmpSize
            tmp = Entry.Substring(0, tmpSize - 1)
            objEventLog.WriteEntry(tmp, EventLogEntryType.Information)
            Debug.WriteLine(tmp.Length.ToString)
            Entry = Entry.Substring(tmpSize)
        Loop
        tmp = Entry
        objEventLog.WriteEntry(tmp, EventLogEntryType.Information)
    End Sub

End Module
like image 872
penright Avatar asked Jan 15 '15 22:01

penright


2 Answers

We currently have the problem too. I'm not sure if this is the solution and the official information about this error is about floppy disks instead of log messages: https://technet.microsoft.com/en-us/library/cc978749.aspx

But I found out that at Log4Net an error was reported that the log file can become corrupt when writing log messages over around 30.000 characters. https://issues.apache.org/jira/browse/LOG4NET-360

We will now recreate the log files and cut the string before writing a new log entry. Fingers crossed that this helps (comments appreciated).

like image 152
CodingYourLife Avatar answered Nov 15 '22 10:11

CodingYourLife


I did the same as Dess said and it worked for me when I reduced the max log entry size to 25000 characters.

The earlier message A device attached to the system is not functioning seems to be misleading.

like image 30
sajid Avatar answered Nov 15 '22 10:11

sajid