0

I am following this article: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-3.1

I am trying to only log my own custom logging in a asp.net core 3.1 API. And not all logs generated from asp.net core. I have created a blank weather forecast service:

[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
    _logger.LogTrace("LogTrace");
    _logger.LogDebug("LogDebug");
    _logger.LogInformation("LogInformation");
    _logger.LogError("LogError");

    var rng = new Random();
    return Enumerable.Range(1, 5).Select(index => new WeatherForecast
    {
        Date = DateTime.Now.AddDays(index),
        TemperatureC = rng.Next(-20, 55),
        Summary = Summaries[rng.Next(Summaries.Length)]
    })
    .ToArray();
}

appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Trace",
      "Microsoft": "Trace",
      "TestAPI": "Trace"
    },
    "ApplicationInsights": {
      "InstrumentationKey": "xx-xx-x-x-xx",
      "LogLevel": {
        "Default": "Trace",
        "Microsoft": "Trace",
        "TestAPI": "Trace"
      }
    }
  },
  "AllowedHosts": "*"
}

I have following nuget installed both version 2.14.0: Microsoft.Extensions.Logging.ApplicationInsights Microsoft.ApplicationInsights.AspNetCore

Now I try to run the app, but gets no logs.

I try adding services.AddApplicationInsightsTelemetry(); to startup:

public void ConfigureServices(IServiceCollection services)
{
    services.AddApplicationInsightsTelemetry();
    services.AddControllers();
}

Same no logs.

Thomas Segato
  • 4,567
  • 11
  • 55
  • 104

2 Answers2

1

First, please note that the ApplicationInsights key means two very different things depending on where it's located in the JSON file.

If the key is on the JSON root level (ie. what you call "outside"), it's used to configure Application Insights, and it's where you specify your instrumentation key. It looks like this:

{
  "ApplicationInsights": {
    "Instrumentationkey":"xxx-36a5-4687-b1fc-xxxxxx"
  }
}

Second, if it's located inside the Logging section, it's used to configure the ApplicationInsightsLoggerProvider, which determines which log level is sent to Application Insights. That's the ILogger log filtering mechanism.

By default, only log levels warning or higher are sent to app insights. If you only want to send all your logs to application insights, you can either configure it for your namespaces, or ignore the messages coming from the System and Microsoft namespaces:

{
  "Logging": {
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Trace"
        "System": "None",
        "Microsoft": "None"
      }
    }
}
Métoule
  • 13,062
  • 2
  • 56
  • 84
0

First of all, there is no need to put those provider settings outside "Logging" (you shouldn't).
Every logging setting you want should be put inside only ( unless there is a provider configured to specifically read that)

Now to answer your question, let's say that your application's root namespace is MyNetCore. ( it would be similar to the name of the project).

If you are using Visual Studio, You can view your Project's root namespace from Project Properties -> Application -> Default Namespace

To view logs from your application only, you have to set the default logging level to None and logging level of your project MyNetCore to Trace
[Edit: You have set the logging levels for ApplicationInsights (or any other provider) separately. The default one is for kestrel.]

"Logging": {
    "LogLevel": {
        "Default": "None",
        "MyNetCore": "Trace"
    },
    "ApplicationInsights": {
        "InstrumentationKey": "xxx-36a5-4687-b1fc-xxxxxx",
        "LogLevel": {
            "Default": "None",
            "MyNetCore": "Trace"
        }
    }
}

The Logging level set here is the minimum level.
If you set it to Trace (0), all the log levels (greater than 0) will be shown. i.e. From Information to Critical
If you set it to None (6), no logs will be shown
Reference to different Log Levels : https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.loglevel

If you want to view only errors from asp.net core, and every log level from your application, then you can do this.

"Logging": {
    "LogLevel": {
        "Default": "None",
        "Microsoft": "Error",
        "MyNetCore": "Trace"
    },
    "ApplicationInsights": {
        "InstrumentationKey": "xxx-36a5-4687-b1fc-xxxxxx",
        "LogLevel": {
            "Default": "None",
            "Microsoft": "Error",
            "MyNetCore": "Trace"
        }
    }
}

Edit: To read the above ApplicationInsights configuration, you need to have Microsoft.Extensions.Logging.ApplicationInsights nuget package installed. Otherwise the config will be totally ignored.

  • Thanks I just tested. With following i dont get any logs. { "Logging": { "LogLevel": { "Default": "None", "Microsoft": "Error", "VendorFormAPI": "Trace" }, "ApplicationInsights": { "InstrumentationKey": "xxx", "LogLevel": { "Default": "Trace" } } }, – Thomas Segato May 18 '20 at 17:12
  • Could be related to my configuration then. I added by Startup.cs. – Thomas Segato May 18 '20 at 17:18
  • You have to set the log levels for ApplicationInsights separately. `{"Logging":{"LogLevel":{"Default":"None","Microsoft":"Error","VendorFormAPI":"Trace"},"ApplicationInsights":{"InstrumentationKey":"xxx-36a5-4687-b1fc-xxxxxx","LogLevel":{"Default":"None","Microsoft":"Error","VendorFormAPI":"Trace"}}}}` And as for startup.cs, you don't need to do anything else to setup logging.
    But yeah, make sure you have `Microsoft.Extensions.Logging.ApplicationInsights` nuget package installed to read from the config file. Otherwise it won't work.
    –  May 18 '20 at 18:42
  • I feel I have tried all combinations :D I have this installed Microsoft.ApplicationInsights.AspNetCore. I removed everything from startup related to insights. I have following "Logging": { "LogLevel": { "Default": "None", "Microsoft": "Error", "VendorFormAPI": "Trace" }, "ApplicationInsights": { "InstrumentationKey": "", "LogLevel": { "Default": "None", "Microsoft": "Error", "VendorFormAPI": "Trace" } } }, Still no logs:( Only time I have logs if I have both inside and outside logging. But then I get everything logged. – Thomas Segato May 18 '20 at 19:57
  • And Microsoft.Extensions.Logging.ApplicationInsights – Thomas Segato May 18 '20 at 20:08
  • Even this does not provide any log: "Logging": { "LogLevel": { "Default": "Trace", "Microsoft": "Trace", "VendorFormAPI": "Trace" }, "ApplicationInsights": { "InstrumentationKey": "xx", "LogLevel": { "Default": "Trace", "Microsoft": "Trace", "VendorFormAPI": "Trace" } } }, – Thomas Segato May 18 '20 at 20:17
  • I started all over on a blank weather template. I changed my original post to reflect the steps I have done. – Thomas Segato May 18 '20 at 20:36
  • Thanks for all your help. However the other answer helped in my case. Key needs to be at root level. – Thomas Segato May 18 '20 at 21:08