【问题标题】:System.Diagnostics.EventLog - A device attached to the system is not functioningSystem.Diagnostics.EventLog - 连接到系统的设备无法运行
【发布时间】:2015-03-14 11:07:57
【问题描述】:

我做了一些谷歌搜索,没有一个“设备附加”错误与事件日志有关。我也在 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 之间。从最重要的价值开始并向下移动,我打赌它在 31000 - 31900 范围内。

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

【问题讨论】:

    标签: .net windows-7 windows-xp custom-eventlog


    【解决方案1】:

    我按照 Dess 所说的做了同样的事情,当我将最大日志条目大小减少到 25000 个字符时,它对我有用。

    较早的消息A device attached to the system is not functioning 似乎具有误导性。

    【讨论】:

      【解决方案2】:

      我们目前也有这个问题。我不确定这是否是解决方案,有关此错误的官方信息是关于软盘而不是日志消息: https://technet.microsoft.com/en-us/library/cc978749.aspx

      但我发现在 Log4Net 上报告了一个错误,即当写入超过 30.000 个字符的日志消息时,日志文件可能会损坏。 https://issues.apache.org/jira/browse/LOG4NET-360

      我们现在将重新创建日志文件并在写入新日志条目之前剪切字符串。祈祷这会有所帮助(cmets 赞赏)。

      【讨论】:

      • 起初我有错误'日志条目字符串太长。写入事件日志的字符串不能超过 32766 个字符。',在将其截断为 31000 后,我开始遇到与你们相同的错误,我开始将其截断为 25000,现在它工作正常,所以谢谢。
      猜你喜欢
      • 1970-01-01
      • 2021-08-15
      • 2012-08-18
      • 2011-03-06
      • 2017-02-17
      • 2020-12-14
      • 2013-07-30
      • 2021-03-03
      • 2018-10-09
      相关资源
      最近更新 更多