我正在使用 VB.NET 进行编写,并尝试将 WPF 应用程序的日志记录与 NLog 和 Azure Application Insights 连接起来。我已经设置好了一切,它在调试模式下运行得很好。但是,当我切换到发布模式或部署它时,它不起作用。在 NLog 日志文件中,我收到以下错误:
Failed to create Target with unknown type-alias: ApplicationInsightsTarget - Verify type-alias and check extension is included
。我使用了以下软件包:
Microsoft.ApplicationInsights
Microsoft.ApplicationInsights.NLogTarget
Microsoft.Extensions.Logging
NLog.Extensions.Logging
NLog
但是,在发布模式下,它写入 NLog 文件没有任何问题。 .dll 文件在发布和调试模式下都存在于 bin 文件夹中。
但是,在发布模式下,它写入 NLog 文件没有任何问题。 .dll 文件在发布和调试模式下都存在于 bin 文件夹中
{
"ActivateExtendedLogging": true,
"NLog": {
"internalLogLevel": "Info",
"internalLogFile": "${basedir}/InternalLogFile/LoggingMessages-nlog.txt",
"targets": {
"aiTarget": {
"type": "ApplicationInsightsTarget",
"name": "aiTarget",
"layout": "${message}",
"instrumentationKey": "0000-000000000-00000-00000"
},
"async": true,
"logfile": {
"type": "File",
"fileName": "${basedir}/NLogs/nlog-${shortdate}.log",
"layout": {
"type": "JsonLayout",
"Attributes": [
{
"name": "timestamp",
"layout": "${date:format=o}"
},
{
"name": "level",
"layout": "${level}"
},
{
"name": "logger",
"layout": "${logger}"
},
{
"name": "message",
"layout": "${message:raw=true}"
},
{
"name": "properties",
"encode": false,
"layout": {
"type": "JsonLayout",
"includeallproperties": "true"
}
}
]
}
},
"console": {
"type": "LimitingWrapper",
"interval": "00:00:01",
"messageLimit": 100,
"target": {
"type": "ColoredConsole",
"layout": "${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|${callsite}",
"rowHighlightingRules": [
{
"condition": "level == LogLevel.Error",
"foregroundColor": "Red"
},
{
"condition": "level == LogLevel.Fatal",
"foregroundColor": "Red",
"backgroundColor": "White"
}
],
"wordHighlightingRules": [
{
"regex": "on|off",
"foregroundColor": "DarkGreen"
},
{
"condition": "level == LogLevel.Debug",
"text": "[TEST]",
"foregroundColor": "Blue"
}
]
}
}
},
"rules": [
{
"logger": "*",
"minLevel": "Error",
"writeTo": "logfile"
},
{
"logger": "*",
"minLevel": "Trace",
"writeTo": "aiTarget"
},
{
"logger": "*",
"minLevel": "Debug",
"writeTo": "console"
}
]
}
}
这是代码
Dim PathToJson As String = $"{AppDomain.CurrentDomain.BaseDirectory}bin"
Dim jsonFilePath As String = Path.Combine(PathToJson, "nlogsettings.json")
LogError("", jsonFilePath)
LogError("", AppDomain.CurrentDomain.BaseDirectory)
If File.Exists(jsonFilePath) Then
Dim config = New ConfigurationBuilder() _
.SetBasePath(Directory.GetCurrentDirectory()) _
.AddJsonFile(jsonFilePath, optional:=False, reloadOnChange:=True) _
.Build()
LogManager.Configuration = New NLogLoggingConfiguration(config.GetSection("NLog"))
Dim activateLogging As Boolean
If Boolean.TryParse(config("ActivateExtendedLogging"), ActivateExtendedLogging) Then
Me.ActivateExtendedLogging = ActivateExtendedLogging
End If
Else
WriteLog("NLog Json file does not exist!")
End If
无法创建类型别名未知的目标:ApplicationInsightsTarget - 验证类型别名并检查是否包含扩展名
要包含 NLog 扩展,您必须执行以下操作:
第一部分您已完成,但另一部分可以在 appsettings.json:
中完成 "NLog": {
"extensions": [
{ "assembly": "Microsoft.ApplicationInsights.NLogTarget" }
],
您也可以通过代码注册:
NLog.LogManager.Setup().SetupExtensions(ext =>
ext.RegisterTarget<Microsoft.ApplicationInsights.NLogTarget.ApplicationInsightsTarget >("ApplicationInsightsTarget")
);