使用 VB.Net 的 nlog 日志映射

nlog Logging Map using VB.Net

我在我的 C# 项目中使用了 nlog 并且它作为魅力工作,但现在我需要在 VB.NET 项目中使用它并且当我转换时我不断收到此错误的代码:

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.  

这是我的C#代码:

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; }
    }

这是 VB.NET 版本:

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

注意 这个错误在 ClassLogger.Info ... 谁能帮我找到我遇到这个错误的方法。 先感谢您。

在 VB.NET 中,您需要使用 AddressOf operator to create a function delegate - Action(Of T) 只是一个预定义的委托 - 因此代码应如下所示:

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} _
            })