【发布时间】:2016-06-13 20:11:08
【问题描述】:
我使用的是 Enterprise library 5.0,实际上是日志记录块。在某些情况下,如果与远程端点的连接不成功,我会处理以下异常:
Public Function Connect(ByVal sender As Socket) As Integer
Dim result As Integer
result = -1
Try
' Connect the socket to the remote endpoint.
sender.Connect(remoteEP)
Logger.Write("Socket connected to remote endpoint {0}", _
sender.RemoteEndPoint.ToString())
result = 0
Catch ... another exception
Catch ... another exception
Catch ex As System.Net.Sockets.SocketException
Using New Tracer(Common.LOGGING_SOCKETCONNECTIONERROR)
Dim contextInfo As IDictionary = New Hashtable()
contextInfo.Add("Additional Info (Stack Trace)", ex.StackTrace)
Dim provider As DebugInformationProvider = New DebugInformationProvider()
provider.PopulateDictionary(contextInfo)
Dim logEntry As LogEntry = New LogEntry()
logEntry.Categories.Add(Common.LOGGING_CRITICAL_ERRORS_CATEGORY)
logEntry.Message = String.Format("An error occurred when attempting to access the socket: {0}", ex.Message)
logEntry.ExtendedProperties = contextInfo
Logger.Write(logEntry)
End Using
End Try
Return result
End Function
当上述异常被触发时,我得到一个转换错误:
provider.PopulateDictionary(contextInfo)
转换错误如下:
System.InvalidCastException was unhandled
Message="Unable to cast object of type 'System.Collections.Hashtable' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'."
我做错了什么?
【问题讨论】:
-
您的函数 PopulateDictionary 是否期望 IDictionary 或 IDictionary(of TKey, TValue)?我相信它应该期待 IDictionary
-
好吧,PopulateDictionary 有以下签名:Public Sub PopulateDictionary(ByVal dict As System.Collections.Generic.IDictionary(Of String, Object))
-
并且 contextInfo 是一个哈希表,实现 System.Collection.IDictionary...所以更改函数的签名,一切都应该正常...
-
@Martin 您的意思是更改 PopulateDictionary 签名吗?据我所知这是不可能的,这是唯一可能的签名。
标签: vb.net logging casting exception-handling enterprise-library-5