nlog 使用 VB.Net 的日志记录映射
本文关键字:日志 记录 映射 Net 使用 VB nlog | 更新日期: 2023-09-27 18:31:51
我在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
运算符来创建函数委托 - 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} _
})