问题
I used nlog in my C#
project and it's working as charm, but now I need to use it in VB.NET
project and when I convert the code I keep getting this Error:
Overload resolution failed because no accessible 'Info' accepts this number of arguments.
Overload resolution failed because no accessible 'Debug' accepts this number of arguments.
Overload resolution failed because no accessible 'Error' accepts this number of arguments.
Overload resolution failed because no accessible 'Fatal' accepts this number of arguments.
Overload resolution failed because no accessible 'Warn' accepts this number of arguments.
This is my C#
Code :
private static readonly Logger ClassLogger = LogManager.GetCurrentClassLogger();
private static readonly Lazy<Dictionary<TraceLevel, Action<string>>> LoggingMap = new Lazy<Dictionary<TraceLevel, Action<string>>>(() => new Dictionary<TraceLevel, Action<string>> { { TraceLevel.Info, ClassLogger.Info }, { TraceLevel.Debug, ClassLogger.Debug }, { TraceLevel.Error, ClassLogger.Error }, { TraceLevel.Fatal, ClassLogger.Fatal }, { TraceLevel.Warn, ClassLogger.Warn } });
private Dictionary<TraceLevel, Action<string>> Logger
{
get { return LoggingMap.Value; }
}
and this is VB.NET
version:
Private Shared ReadOnly ClassLogger As Logger = LogManager.GetCurrentClassLogger()
Private Shared ReadOnly LoggingMap As New Lazy(Of Dictionary(Of TraceLevel, Action(Of String))) _
(Function() New Dictionary(Of TraceLevel, Action(Of String)) From _
{{TraceLevel.Info, ClassLogger.Info}, _
{TraceLevel.Debug, ClassLogger.Debug}, _
{TraceLevel.Error, ClassLogger.Error}, _
{TraceLevel.Fatal, ClassLogger.Fatal}, _
{TraceLevel.Warn, ClassLogger.Warn} _
})
Private ReadOnly Property Logger() As Dictionary(Of TraceLevel, Action(Of String))
Get
Return LoggingMap.Value
End Get
End Property
Note this Errors rise in ClassLogger.Info
...
Can any one help me to find way I'm getting this error.
thank you in advance.
回答1:
In VB.NET, you need to use the AddressOf operator to create a function delegate - Action(Of T) is just a predefined delegate - so the code should look like:
Private Shared ReadOnly LoggingMap As New Lazy(Of Dictionary(Of TraceLevel, Action(Of String))) _
(Function() New Dictionary(Of TraceLevel, Action(Of String)) From _
{{TraceLevel.Info, AddressOf ClassLogger.Info}, _
{TraceLevel.Debug, AddressOf ClassLogger.Debug}, _
{TraceLevel.Error, AddressOf ClassLogger.Error}, _
{TraceLevel.Fatal, AddressOf ClassLogger.Fatal}, _
{TraceLevel.Warn, AddressOf ClassLogger.Warn} _
})
来源:https://stackoverflow.com/questions/35013379/nlog-logging-map-using-vb-net