如何通过 NLog 在 AppInsights 中获取范围信息?
How to get scoped information in AppInsights via NLog?
考虑在构造函数中注入 ILogger<>
的方法中的以下代码:
using (_logger.BeginScope("Requesting {page} for {identification}", page, identification))
{
if (identification == null)
{
var test = "Test String";
_logger.LogTrace("No identification present {test}. Presenting Index page", test);
return Page();
}
_logger.LogDebug("Identification present: {identification}", identification);
}
我在 AI 中看到了这一行:
我希望获得有关范围的一些信息;虽然我不知道它是什么样子,但我认为它已添加到 LoggerName
和 test
属性的 CustomDimensions 中。
在我的初创公司 class 我有这个:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<ApplicationInsightsServiceOptions>(Configuration.GetSection("ApplicationInsights"));
services.AddApplicationInsightsTelemetry();
LogManager.Configuration = new NLogLoggingConfiguration(Configuration.GetSection("NLog"));
/// ... the reset
}
我的 appsettings.json 看起来像这样:
{
"Logging": {
"IncludeScopes": true,
"NLog": {
"IncludeScopes": true
},
"LogLevel": {
"Default": "Trace",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
},
"ApplicationInsights": {
"IncludeScopes": true,
"LogLevel": {
"Default": "Trace"
}
}
},
"NLog": {
"autoReload": true,
"throwConfigExceptions": true,
"internalLogLevel": "trace",
"internalLogFile": "${basedir}/internal-nlog.txt",
"extensions": [
{
"assembly": "Microsoft.ApplicationInsights.NLogTarget"
}
],
"targets": {
"aiTarget": {
"type": "ApplicationInsightsTarget"
},
"logconsole": {
"type": "ColoredConsole"
}
},
"rules": [
{
"logger": "*",
"minLevel": "Trace",
"writeTo": "aiTarget, logconsole"
}
]
}
有没有我遗漏的东西...?
我想你可以试试这个:
"targets": {
"aiTarget": {
"type": "ApplicationInsightsTarget",
"contextproperties": [
{
"name": "scopecontext",
"layout": {
"type": "JsonLayout",
"includemdlc": "true"
}
}]
},
"logconsole": {
"type": "ColoredConsole"
}
},
还创建了这个:https://github.com/microsoft/ApplicationInsights-dotnet/pull/2103
考虑在构造函数中注入 ILogger<>
的方法中的以下代码:
using (_logger.BeginScope("Requesting {page} for {identification}", page, identification))
{
if (identification == null)
{
var test = "Test String";
_logger.LogTrace("No identification present {test}. Presenting Index page", test);
return Page();
}
_logger.LogDebug("Identification present: {identification}", identification);
}
我在 AI 中看到了这一行:
我希望获得有关范围的一些信息;虽然我不知道它是什么样子,但我认为它已添加到 LoggerName
和 test
属性的 CustomDimensions 中。
在我的初创公司 class 我有这个:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<ApplicationInsightsServiceOptions>(Configuration.GetSection("ApplicationInsights"));
services.AddApplicationInsightsTelemetry();
LogManager.Configuration = new NLogLoggingConfiguration(Configuration.GetSection("NLog"));
/// ... the reset
}
我的 appsettings.json 看起来像这样:
{
"Logging": {
"IncludeScopes": true,
"NLog": {
"IncludeScopes": true
},
"LogLevel": {
"Default": "Trace",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
},
"ApplicationInsights": {
"IncludeScopes": true,
"LogLevel": {
"Default": "Trace"
}
}
},
"NLog": {
"autoReload": true,
"throwConfigExceptions": true,
"internalLogLevel": "trace",
"internalLogFile": "${basedir}/internal-nlog.txt",
"extensions": [
{
"assembly": "Microsoft.ApplicationInsights.NLogTarget"
}
],
"targets": {
"aiTarget": {
"type": "ApplicationInsightsTarget"
},
"logconsole": {
"type": "ColoredConsole"
}
},
"rules": [
{
"logger": "*",
"minLevel": "Trace",
"writeTo": "aiTarget, logconsole"
}
]
}
有没有我遗漏的东西...?
我想你可以试试这个:
"targets": {
"aiTarget": {
"type": "ApplicationInsightsTarget",
"contextproperties": [
{
"name": "scopecontext",
"layout": {
"type": "JsonLayout",
"includemdlc": "true"
}
}]
},
"logconsole": {
"type": "ColoredConsole"
}
},
还创建了这个:https://github.com/microsoft/ApplicationInsights-dotnet/pull/2103