我做了一些谷歌,没有一个“设备附加”错误与事件日志有任何关系。我也在 Micosoftfourm 上发布了这个问题,但没有任何回应。以为我会给你们一个机会。这里是问题的链接。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
这是错误消息:
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:
这段代码在一个库中,所以我创建了一个“hello world”来演示这个问题。我会在最后贴出来。这个 .net 4 框架在我们的代码中已经存在了一段时间。我们开始从 XP 升级到 Win 7。基本上事件日志的大小被限制在 32667 之内。所以我们做了一个测试,如果字符串比那个大,那么我们就用 32000 字节的卡盘来写。在两个不同的 win7 机器上,我们收到“设备已连接”错误消息。在 XP 机器上运行相同的代码,它就可以工作。哦,Win 7 的盒子是 64 位的。我想知道win 7 32位是否会有同样的问题?其他人可以复制吗?tmpsize 似乎是不同的数字,但如果您使用它,您可以将其归结为数字 x 有效,而 (x+1) 无效。介于 30000 和 32000 之间。从最重要的价值开始,然后向下移动,
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