1

Consider the following code in a method where an ILogger<> is injected in the constructor:

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

I see this line in AI: enter image description here

where I'd expect some information regarding the scope; although I don't know how it looks like but I assume it's added to the CustomDimensions along the LoggerName and test properties.

In my startup class I have this:

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<ApplicationInsightsServiceOptions>(Configuration.GetSection("ApplicationInsights"));
    services.AddApplicationInsightsTelemetry();
    LogManager.Configuration = new NLogLoggingConfiguration(Configuration.GetSection("NLog"));

    /// ... the reset
}

My appsettings.json looks like this:

{
  "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"
      }
    ]
  }

Is there something that I'm missing...?

321X
  • 3,153
  • 2
  • 30
  • 42

1 Answers1

3

I guess you can try this:

    "targets": {
      "aiTarget": {
        "type": "ApplicationInsightsTarget",
        "contextproperties": [
        {
            "name": "scopecontext",
            "layout": {
                "type": "JsonLayout",
                "includemdlc": "true"
            }
        }]
      },
      "logconsole": {
        "type": "ColoredConsole"
      }
    },

Have also created this: https://github.com/microsoft/ApplicationInsights-dotnet/pull/2103

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70
  • Wow, it works! I'm very grateful for your answer :-) but I don't see how I could have possibly solved this without your knowledge, it maybe purely experience. Is your PR to make it out-of-the-box available like the docs imply? So, if the PR is approved the config changes can be reverted to have the same functionality? – 321X Oct 31 '20 at 19:12
  • @321X Yes the PR will allow capture of scope-context without the help from JsonLayout – Rolf Kristensen Nov 01 '20 at 14:14