.net - System.Diagnostics.EventLog - 连接到系统的设备无法运行

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

我做了一些谷歌,没有一个“设备连接”错误与事件日志有任何关系。我也在微软论坛上发布了这个问题,但没有回复。我想我会给你们一个机会。这是问题的链接。 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

最佳答案

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

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

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

关于.net - System.Diagnostics.EventLog - 连接到系统的设备无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27973896/

相关文章:

c# - Windows 7 任务栏图标高亮颜色

silverlight - Windows Phone 7 模拟器是否显示水印?

c - 如何多播(发送)到第一个网卡?

java - java中memcached中设置操作的IOException

java - 数据源与简单连接数据库

c# - Nant 无法构建 c# .net 项目

c# - 使用依赖注入(inject)将 token 获取从startup.cs传递到HomeController.cs

windows-7 - 从添加/删除程序中删除不良安装

networking - 从命令行启用/禁用网络连接

.net - 如何解决 JScript.NET 程序集引用错误 JS1259?